{"id":276,"date":"2025-11-26T02:13:22","date_gmt":"2025-11-26T02:13:22","guid":{"rendered":"https:\/\/haco.club\/?p=276"},"modified":"2025-11-26T02:14:47","modified_gmt":"2025-11-26T02:14:47","slug":"global-variable-declaration-inside-a-function-in-python","status":"publish","type":"post","link":"https:\/\/haco.club\/?p=276","title":{"rendered":"Global variable declaration inside function (Python"},"content":{"rendered":"\n<p>If you only want to <strong>read<\/strong> (access) the value of a global variable, you do <strong>not<\/strong> need the global keyword. Python automatically looks up the variable in the global scope if it doesn&#8217;t find it locally.<\/p>\n\n\n\n<p>However, the global keyword is <strong>mandatory if you want to modify (write to)<\/strong> that variable.<\/p>\n\n\n\n<p>Here is the breakdown of why it exists.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Reading (No global needed)<\/h3>\n\n\n\n<p>As you noticed, this works fine. Python looks for count locally, fails, looks globally, and finds it. code Pythondownloadcontent_copyexpand_less<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    <code>count = 10\n\ndef read_variable():\n    # Works perfectly without 'global'\n    print(count) \n\nread_variable() # Output: 10<\/code>\n  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Writing WITHOUT global (The Trap)<\/h3>\n\n\n\n<p>If you try to assign a value to a variable inside a function, Python assumes you are creating a <strong>new local variable<\/strong> with the same name. It <strong>does not<\/strong> touch the global variable. This is called &#8220;shadowing.&#8221; code Pythondownloadcontent_copyexpand_less<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    <code>count = 10\n\ndef try_to_change():\n    count = 99  # Python creates a NEW local variable named 'count'\n    print(f\"Inside: {count}\")\n\ntry_to_change()   # Output: Inside: 99\nprint(f\"Outside: {count}\") # Output: Outside: 10 (The global one didn't change!)<\/code>\n  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Writing WITH global (The Fix)<\/h3>\n\n\n\n<p>If you want to change the variable that lives <em>outside<\/em> the function, you must explicitly tell Python, &#8220;I am referring to the global variable, not making a new one.&#8221; code Pythondownloadcontent_copyexpand_less<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    <code>count = 10\n\ndef actually_change():\n    global count  # Tell Python to use the outside variable\n    count = 99    # Now this updates the global variable\n    print(f\"Inside: {count}\")\n\nactually_change() # Output: Inside: 99\nprint(f\"Outside: {count}\") # Output: Outside: 99 (It changed!)<\/code>\n  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. The Crash (UnboundLocalError)<\/h3>\n\n\n\n<p>This is the most common error people encounter. If you try to <strong>read and write<\/strong> the variable in the same function without global, Python gets confused.<\/p>\n\n\n\n<p>Because there is an assignment (count += 1) anywhere in the function, Python decides count is <strong>local<\/strong>. But since you try to read it before assigning it, it crashes. code Pythondownloadcontent_copyexpand_less<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    <code>count = 10\n\ndef increment():\n    # ERROR! UnboundLocalError: local variable 'count' referenced before assignment\n    count = count + 1 \n\nincrement()<\/code>\n  <\/pre>\n\n\n\n<p><strong>The Fix:<\/strong> code Pythondownloadcontent_copyexpand_less<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    <code>def increment():\n    global count\n    count = count + 1 # Now it works<\/code>\n  <\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reading only:<\/strong> global is <strong>not<\/strong> required.<\/li>\n\n\n\n<li><strong>Assigning\/Updating:<\/strong> global <strong>is<\/strong> required.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you only want to read (access) the value of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[47],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-knowledge-base","tag-python"],"_links":{"self":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":3,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":280,"href":"https:\/\/haco.club\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions\/280"}],"wp:attachment":[{"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haco.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}