{"id":4528,"date":"2023-12-15T11:27:51","date_gmt":"2023-12-15T11:27:51","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4528"},"modified":"2023-12-15T12:16:56","modified_gmt":"2023-12-15T12:16:56","slug":"static-variables-inside-a-function","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/memory-python\/static-variables-inside-a-function","title":{"rendered":"static variables inside a function"},"content":{"rendered":"\n<p>In C a static variable inside a function is only visible inside that function&#8217;s scope, but its lifetime is the entire life of the program, and it&#8217;s only initialized once.<\/p>\n\n\n\n<p>There is NO equivalence in Python.<\/p>\n\n\n\n<p>So if you want to be a proper python programmer you should just accept that! Use a class, use a global variable, etc.<br>However there are workarounds if you&#8217;ve got a really good reason to want to use variables in a static way:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_function():\n    my_function.my_static_variable+= 1\n    print (\"Value is \" + str(my_function.my_static_variable))\n\n#static variables initialisation\nmy_function.my_static_variable= 3<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating static variables at the top of a function<\/h4>\n\n\n\n<p>A useful way of being able to initialize your static variables at the top of a function (much clear to read and understand for long functions)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#**********************************************\n#***** STATIC VARIABLE DECORATOR FUNCTION *****\n#**********************************************\ndef create_static_variable(**kwargs):\n    def decorate(func):\n        for k in kwargs:\n            setattr(func, k, kwargs&#91;k])\n        return func\n    return decorate\n#You can now do this for static variables inside a function:\n#@create_static_variable(my_static_variable = 0)    #&lt;&lt;&lt;The @ symbol is a decorator and passes the function following it to the call\n#                                                   #&lt;&lt;&lt;Notice the function name is not included here (but must be when it is used in the function)\n#def my_function():\n#    my_function.my_static_variable += 1\n#    print (\"Value now is \" + str(my_function.my_static_variable))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In C a static variable inside a function is only visible inside that function&#8217;s scope, but its lifetime is the entire life of the program, and it&#8217;s only initialized once. There is NO equivalence in Python. So if you want to be a proper python programmer you should just accept that! Use a class, use [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[335,320],"tags":[],"class_list":["post-4528","post","type-post","status-publish","format-standard","hentry","category-functions","category-memory-python"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4528"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4528\/revisions"}],"predecessor-version":[{"id":4537,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4528\/revisions\/4537"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}