{"id":531,"date":"2024-12-25T08:08:01","date_gmt":"2024-12-25T08:08:01","guid":{"rendered":"https:\/\/datadandies.nl\/?p=531"},"modified":"2024-12-25T08:08:01","modified_gmt":"2024-12-25T08:08:01","slug":"variable-accessibility-in-for-loops-vs-functions-in-python-local-scope","status":"publish","type":"post","link":"https:\/\/datadandies.nl\/index.php\/2024\/12\/25\/variable-accessibility-in-for-loops-vs-functions-in-python-local-scope\/","title":{"rendered":"Variable accessibility in for loops vs functions in Python &#8211; local scope"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Merry Christmas my dear network. Ready for some Python on Christmas day? Here we go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the risk of kicking in open doors: don\u2019t confuse the semantics of variable declaration in for-loops with functions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables that are defined in a function, are local to that function. They exist in the so called \u201clocal scope\u201d of the function and are not accessible outside of that function (unless you define the variable to be global, but let\u2019s skip over that for the sake of keeping this post short).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Variables that are defined in a for-loop \u00e1re accessible outside of the for-loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Details and an example of this can be found below. <\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Variables defined within a for loop are still accessible after the loop ends\n# The same is not true for functions. In a function variables are defined in a &quot;local scope&quot; (only available inside the function). &quot;Local variables&quot; are not accessible in the &quot;global scope&quot; (outside of the function)\n\n# Creating a loop with variables &quot;z&quot; and &quot;f&quot;\nfor z in range(5):\n    f = z+1\n\n# Creating a function with variable &quot;woorden&quot; (local scope)\ndef name_function(name: str):\n    woorden = 'Your name is '\n    return print(f'{woorden}{name}')\n\n# Using variables defined in the loop, outside the loop\nprint(z)        # Variable accessible outside of for loop; returns 4\nprint(f)        # Variable accessible outside of for loop; returns 5\n\n# Executing name_function\nname_function('David')   # Returns &quot;Your name is David&quot;\n\n# Trying to access local variable &quot;woorden&quot; from name_function\ntry:\n    print(woorden)                      # This throws an error saying the variable is not defined\nexcept NameError as e:\n    print(f'NameError occurred: {e}.')  # Which results in this line being executed\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Kopieer\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Variables defined within a for loop are still accessible after the loop ends<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># The same is not true for functions. In a function variables are defined in a &quot;local scope&quot; (only available inside the function). &quot;Local variables&quot; are not accessible in the &quot;global scope&quot; (outside of the function)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Creating a loop with variables &quot;z&quot; and &quot;f&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> z <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">range<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    f <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> z<\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #B48EAD\">1<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Creating a function with variable &quot;woorden&quot; (local scope)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">name_function<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">name<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">str<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    woorden <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">Your name is <\/span><span style=\"color: #ECEFF4\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">return<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #A3BE8C\">&#39;<\/span><span style=\"color: #EBCB8B\">{<\/span><span style=\"color: #D8DEE9FF\">woorden<\/span><span style=\"color: #EBCB8B\">}{<\/span><span style=\"color: #D8DEE9FF\">name<\/span><span style=\"color: #EBCB8B\">}<\/span><span style=\"color: #A3BE8C\">&#39;<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Using variables defined in the loop, outside the loop<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">z<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># Variable accessible outside of for loop; returns 4<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">f<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">        <\/span><span style=\"color: #616E88\"># Variable accessible outside of for loop; returns 5<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Executing name_function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">name_function<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #A3BE8C\">David<\/span><span style=\"color: #ECEFF4\">&#39;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">   <\/span><span style=\"color: #616E88\"># Returns &quot;Your name is David&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Trying to access local variable &quot;woorden&quot; from name_function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">try<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">woorden<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">                      <\/span><span style=\"color: #616E88\"># This throws an error saying the variable is not defined<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">except<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">NameError<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">as<\/span><span style=\"color: #D8DEE9FF\"> e<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #A3BE8C\">&#39;NameError occurred: <\/span><span style=\"color: #EBCB8B\">{<\/span><span style=\"color: #D8DEE9FF\">e<\/span><span style=\"color: #EBCB8B\">}<\/span><span style=\"color: #A3BE8C\">.&#39;<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># Which results in this line being executed<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Merry Christmas my dear network. Ready for some Python on Christmas day? Here we go. At the risk of kicking in open doors: don\u2019t confuse the semantics of variable declaration in for-loops with functions. Variables that are defined in a function, are local to that function. They exist in the so called \u201clocal scope\u201d of&hellip;<\/p>\n<p class=\"more-link\"><a href=\"https:\/\/datadandies.nl\/index.php\/2024\/12\/25\/variable-accessibility-in-for-loops-vs-functions-in-python-local-scope\/\" class=\"themebutton\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[37],"class_list":["post-531","post","type-post","status-publish","format-standard","hentry","category-blog","tag-python"],"_links":{"self":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/comments?post=531"}],"version-history":[{"count":1,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/531\/revisions"}],"predecessor-version":[{"id":532,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/posts\/531\/revisions\/532"}],"wp:attachment":[{"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/media?parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/categories?post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datadandies.nl\/index.php\/wp-json\/wp\/v2\/tags?post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}