{"id":11548,"date":"2025-04-14T17:30:48","date_gmt":"2025-04-14T12:00:48","guid":{"rendered":"https:\/\/sourcebae.com\/blog\/?p=11548"},"modified":"2025-04-14T17:30:49","modified_gmt":"2025-04-14T12:00:49","slug":"python-namespace-and-scope-of-a-variable-with-examples","status":"publish","type":"post","link":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/","title":{"rendered":"Python Namespace and Scope of a Variable (With Examples)"},"content":{"rendered":"\n<p>Python, a widely-popular and powerful programming language, is known for its simplicity and readability. Whether you&#8217;re a beginner or an experienced developer, having a clear understanding of Python namespaces and scopes is crucial. The concepts of namespace and scope might sound complex initially, but they are fundamental to writing organized, efficient, and error-free Python code. In this comprehensive guide, you&#8217;ll explore Python namespaces, scopes, the LEGB rule, as well as the useful <code>global<\/code> and <code>nonlocal<\/code> keywords, explained with practical examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-namespace-in-python\">What is a Namespace in Python?<\/h2>\n\n\n\n<p>In Python, a <strong>namespace<\/strong> refers to a container or mapping of names to corresponding objects. A simple analogy is a family name: although you might share your name with another family, your surname (namespace) helps distinguish you clearly. Similarly, namespaces in Python ensure that names are unique and won&#8217;t conflict with one another within the same scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"why-namespaces-are-necessary\">Why Namespaces Are Necessary?<\/h3>\n\n\n\n<p>Namespaces are essential because they allow Python to maintain clarity and organization. By separating names into different &#8220;virtual rooms,&#8221; you can have identical names referring to different objects in separate namespaces without confusion or conflict.<\/p>\n\n\n\n<p>Python primarily has three kinds of namespaces:<\/p>\n\n\n\n<p>Check out: <a href=\"https:\/\/sourcebae.com\/blog\/what-is-r-in-python-purpose-usage-explained\/\">What is R in Python<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"types-of-python-namespaces\">Types of Python Namespaces:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Built-in Namespace<\/strong>: Contains all Python built-in functions and exceptions such as <code>print()<\/code>, <code>input()<\/code>, <code>len()<\/code>, and <code>range()<\/code>.<\/li>\n\n\n\n<li><strong>Global Namespace<\/strong>: Includes names defined at the module or script level.<\/li>\n\n\n\n<li><strong>Local Namespace<\/strong>: Holds names defined within functions or blocks.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-namespace-examples\">Python Namespace Examples<\/h2>\n\n\n\n<p>To understand Python namespaces clearly, let&#8217;s explore a few examples.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"built-in-namespace-example\">Built-in Namespace Example:<\/h3>\n\n\n\n<p>The built-in namespace is inherent and available by default:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(len(\"Hello, World!\"))\n<\/code><\/pre>\n\n\n\n<p>Here, <code>print()<\/code> and <code>len()<\/code> belong to Python&#8217;s built-in namespace, accessible directly without explicit importing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"global-namespace-example\">Global Namespace Example:<\/h3>\n\n\n\n<p>Names (variables, functions, and classes) assigned at the script level belong to the global namespace:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\n\ndef print_global_x():\n    print(x)\n\nprint_global_x()  # Output: 10\n<\/code><\/pre>\n\n\n\n<p>Here, variable <code>x<\/code> and the function <code>print_global_x<\/code> reside within the global namespace accessible within the whole script.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"local-namespace-example\">Local Namespace Example:<\/h3>\n\n\n\n<p>Variables created inside functions are part of a local namespace\u2014exclusive to the function itself:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def example_function():\n    y = 20\n    print(y)\n\nexample_function()  # Output: 20\n# print(y)  # Would raise an error (NameError) since y is local to function only\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"lifetime-of-a-python-namespace\">Lifetime of a Python Namespace<\/h2>\n\n\n\n<p>The lifespan of a namespace varies depending on its type:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Built-in namespace<\/strong>: Created when Python starts and destroyed when the interpreter exits (lasts during Python session lifetime).<\/li>\n\n\n\n<li><strong>Global namespace<\/strong>: Initiated upon running the Python script, destroyed when the script finishes execution.<\/li>\n\n\n\n<li><strong>Local namespace<\/strong>: Created with each function call; destroyed immediately after the function returns or ends.<\/li>\n<\/ul>\n\n\n\n<p>Check out: <a href=\"https:\/\/sourcebae.com\/blog\/what-is-r-in-python-purpose-usage-explained\/\">Features of Python<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"scope-of-variables-in-python\">Scope of Variables in Python<\/h2>\n\n\n\n<p>Python variables have scope, which defines the visibility or accessibility of a variable. Scope determines the namespace region to which a variable belongs. While namespaces are containers, scope is essentially a set of rules that determine where variables and names can be accessed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"types-of-scopes-in-python\">Types of Scopes in Python:<\/h3>\n\n\n\n<p>Python has four types of scopes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Local Scope<\/strong>: Includes variables defined within a function.<\/li>\n\n\n\n<li><strong>Enclosing Scope<\/strong>: Pertains to names defined in an enclosing function if nested functions are used.<\/li>\n\n\n\n<li><strong>Global Scope<\/strong>: Variables defined at the module or script level.<\/li>\n\n\n\n<li><strong>Built-in Scope<\/strong>: Python&#8217;s built-ins scope predefined functions.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"understanding-legb-rule\">Understanding LEGB Rule<\/h3>\n\n\n\n<p>Python examines variables following the LEGB Rule, which stands for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>L (Local)<\/strong>: Local function variables first.<\/li>\n\n\n\n<li><strong>E (Enclosing)<\/strong>: Names in enclosing scopes (nested functions).<\/li>\n\n\n\n<li><strong>G (Global)<\/strong>: Global names.<\/li>\n\n\n\n<li><strong>B (Built-in)<\/strong>: Built-in Python functions.<\/li>\n<\/ul>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = \"global x\"\n\ndef outer():\n    x = \"outer x\"\n    def inner():\n        x = \"inner x\"\n        print(x)\n    inner()\n    print(x)\n\nouter()\nprint(x)\n\n# Output:\n# inner x\n# outer x\n# global x\n<\/code><\/pre>\n\n\n\n<p>Python uses this LEGB sequence to locate variables and avoids namespace ambiguity effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-global-and-nonlocal-keywords\">Python <code>global<\/code> and <code>nonlocal<\/code> Keywords<\/h2>\n\n\n\n<p>Sometimes, you&#8217;ll want Python to alter or assign a value to variables outside the current scope. Here&#8217;s where the reserved words <code>global<\/code> and <code>nonlocal<\/code> come into play.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-demonstrating-global-keyword\">Example Demonstrating &#8216;global&#8217; Keyword:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 5\n\ndef modify_global():\n    global x\n    x = 10\n\nmodify_global()\nprint(x)  # Output: 10\n<\/code><\/pre>\n\n\n\n<p>Here, the <code>global<\/code> keyword allows the function to modify a variable from the global namespace.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-demonstrating-nonlocal-keyword\">Example Demonstrating &#8216;nonlocal&#8217; Keyword:<\/h3>\n\n\n\n<p>Using nested functions requires the <code>nonlocal<\/code> keyword to alter variables from the enclosing scope:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer():\n    x = \"local\"\n    def inner():\n        nonlocal x\n        x = \"nonlocal\"\n    inner()\n    print(x)  # Output: nonlocal\n\nouter()\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"common-mistakes-and-best-practices\">Common Mistakes and Best Practices<\/h2>\n\n\n\n<p>Learning namespaces and scopes can prevent common programming pitfalls.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"common-mistakes\">Common Mistakes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Shadowing Built-in Variables<\/strong>: Avoid defining variables named <code>list<\/code>, <code>range<\/code>, or <code>sum<\/code>, as it shadows built-in functions\u2014potentially causing unexpected behavior.<\/li>\n\n\n\n<li><strong>Overusing global variables<\/strong>: Excessive global variables lead to confusion; prefer local or enclosing variables when possible.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"best-practices\">Best Practices:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clearly define variable scopes.<\/li>\n\n\n\n<li>Avoid global variables unless necessary.<\/li>\n\n\n\n<li>Use distinctive and clear naming conventions.<\/li>\n<\/ul>\n\n\n\n<p>Check out: <a href=\"https:\/\/sourcebae.com\/blog\/how-to-convert-bytes-to-a-string-in-python\/\">Convert bytes to a string<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faqs-frequently-asked-questions\">FAQs (Frequently Asked Questions)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-the-difference-between-scope-and-namespace-in-python\">What is the difference between scope and namespace in Python?<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Namespace<\/strong> is a mapping of names to objects.<\/li>\n\n\n\n<li><strong>Scope<\/strong> is a textual region of a Python program where a namespace is directly accessible.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"can-i-have-two-variables-with-the-same-name-in-different-namespaces\">Can I have two variables with the same name in different namespaces?<\/h3>\n\n\n\n<p>Certainly! See this example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = \"global\"\n\ndef func():\n    x = \"local\"\n    print(x)  # local\n\nfunc()\nprint(x)  # global\n<\/code><\/pre>\n\n\n\n<p>Each variable <code>x<\/code> belongs to its own namespace and thus does not interfere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-are-closures-and-how-do-namespaces-interact-with-them\">What are closures and how do namespaces interact with them?<\/h3>\n\n\n\n<p><strong>Closures<\/strong> retain the state of enclosing scope namespaces even after the outer function exits:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer_func(message):\n    def inner_func():\n        print(message)\n    return inner_func  # returning inner function reference\n\nmy_closure = outer_func(\"Hello Closure!\")\nmy_closure()  # Outputs: Hello Closure!\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"does-python-create-namespaces-for-classes-methods-and-modules\">Does Python create namespaces for classes, methods, and modules?<\/h3>\n\n\n\n<p>Yes, every method, class, and module has its own namespace in Python, helping avoid naming conflicts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-can-i-list-all-variables-and-names-in-a-namespace-in-python\">How can I list all variables and names in a namespace in Python?<\/h3>\n\n\n\n<p>Use built-in functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>globals()<\/code>: Global namespace.<\/li>\n\n\n\n<li><code>locals()<\/code>: Current local namespace.<\/li>\n\n\n\n<li><code>dir()<\/code>: Comprehensive list of available names.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"when-does-python-delete-a-namespace-from-memory\">When does Python delete a namespace from memory?<\/h3>\n\n\n\n<p>Python deletes a namespace when the associated scope or context ends. For example, a function&#8217;s local namespace disappears when the execution completes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In this detailed exploration, you&#8217;ve learned about namespaces and scopes in Python, the LEGB rule, and the important keywords: <code>global<\/code> and <code>nonlocal<\/code>. Understanding these concepts clearly and practically enhances the way you structure code, reducing errors and enhancing readability and maintainability.<\/p>\n\n\n\n<p>I encourage you to experiment with examples from this guide to solidify your understanding. Practice with various scenarios to grasp Python namespaces and scopes comprehensively, elevating your coding proficiency.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"additional-resources-and-recommended-reading\">Additional Resources and Recommended Reading<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/classes.html#python-scopes-and-namespaces\">Official Python Documentation on Namespaces and Scope<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/realpython.com\/python-scope-legb-rule\/\">Understanding Python&#8217;s LEGB Rule<\/a><\/li>\n<\/ul>\n\n\n\n<p>Have questions or need clarification? Feel free to comment below! Don\u2019t forget to subscribe to our Python blog tutorial series\u2014packed with in-depth guides, advanced tutorials, and plenty of tips to help you achieve Python mastery.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python, a widely-popular and powerful programming language, is known for its simplicity and readability. Whether you&#8217;re a beginner or an experienced developer, having a clear understanding of Python namespaces and scopes is crucial. The concepts of namespace and scope might sound complex initially, but they are fundamental to writing organized, efficient, and error-free Python code. [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":11555,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,22],"tags":[1890,1887,1885,1889,1886],"class_list":["post-11548","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-all","category-python","tag-namespace-in-python","tag-namespace-python","tag-python-namespace","tag-python-variable","tag-scope-of-a-variable"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Namespace and Scope of a Variable (With Examples) - SourceBae<\/title>\n<meta name=\"description\" content=\"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Namespace and Scope of a Variable (With Examples) - SourceBae\" \/>\n<meta property=\"og:description\" content=\"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"SourceBae\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-14T12:00:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-14T12:00:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1240\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Shubham Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/\",\"url\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/\",\"name\":\"Python Namespace and Scope of a Variable (With Examples) - SourceBae\",\"isPartOf\":{\"@id\":\"https:\/\/sourcebae.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp\",\"datePublished\":\"2025-04-14T12:00:48+00:00\",\"dateModified\":\"2025-04-14T12:00:49+00:00\",\"author\":{\"@id\":\"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/8adda4e97f4595b666220423e6cb0ab8\"},\"description\":\"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.\",\"breadcrumb\":{\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage\",\"url\":\"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp\",\"contentUrl\":\"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp\",\"width\":1240,\"height\":620,\"caption\":\"Python Namespace and Scope of a Variable (With Examples)\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/sourcebae.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Namespace and Scope of a Variable (With Examples)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/sourcebae.com\/blog\/#website\",\"url\":\"https:\/\/sourcebae.com\/blog\/\",\"name\":\"SourceBae\",\"description\":\"Creative Blog Website\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/sourcebae.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/8adda4e97f4595b666220423e6cb0ab8\",\"name\":\"Shubham Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19e56e8a95c968c3c385225fd321161953aefbcf821e45d2e29a39939ed30d5c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19e56e8a95c968c3c385225fd321161953aefbcf821e45d2e29a39939ed30d5c?s=96&d=mm&r=g\",\"caption\":\"Shubham Kumar\"},\"sameAs\":[\"https:\/\/sourcebae.com\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Namespace and Scope of a Variable (With Examples) - SourceBae","description":"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Namespace and Scope of a Variable (With Examples) - SourceBae","og_description":"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.","og_url":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/","og_site_name":"SourceBae","article_published_time":"2025-04-14T12:00:48+00:00","article_modified_time":"2025-04-14T12:00:49+00:00","og_image":[{"width":1240,"height":620,"url":"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp","type":"image\/webp"}],"author":"Shubham Kumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Shubham Kumar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/","url":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/","name":"Python Namespace and Scope of a Variable (With Examples) - SourceBae","isPartOf":{"@id":"https:\/\/sourcebae.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp","datePublished":"2025-04-14T12:00:48+00:00","dateModified":"2025-04-14T12:00:49+00:00","author":{"@id":"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/8adda4e97f4595b666220423e6cb0ab8"},"description":"Learn Python namespace and variable scope with clear examples. Master the basics to write clean, bug-free code faster and smarter.","breadcrumb":{"@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#primaryimage","url":"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp","contentUrl":"https:\/\/sourcebae.com\/blog\/wp-content\/uploads\/2025\/04\/Add-a-heading-2025-04-14T172605.684.webp","width":1240,"height":620,"caption":"Python Namespace and Scope of a Variable (With Examples)"},{"@type":"BreadcrumbList","@id":"https:\/\/sourcebae.com\/blog\/python-namespace-and-scope-of-a-variable-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sourcebae.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Namespace and Scope of a Variable (With Examples)"}]},{"@type":"WebSite","@id":"https:\/\/sourcebae.com\/blog\/#website","url":"https:\/\/sourcebae.com\/blog\/","name":"SourceBae","description":"Creative Blog Website","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sourcebae.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/8adda4e97f4595b666220423e6cb0ab8","name":"Shubham Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/sourcebae.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19e56e8a95c968c3c385225fd321161953aefbcf821e45d2e29a39939ed30d5c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19e56e8a95c968c3c385225fd321161953aefbcf821e45d2e29a39939ed30d5c?s=96&d=mm&r=g","caption":"Shubham Kumar"},"sameAs":["https:\/\/sourcebae.com\/"]}]}},"_links":{"self":[{"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/posts\/11548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/comments?post=11548"}],"version-history":[{"count":1,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/posts\/11548\/revisions"}],"predecessor-version":[{"id":11556,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/posts\/11548\/revisions\/11556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/media\/11555"}],"wp:attachment":[{"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/media?parent=11548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/categories?post=11548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sourcebae.com\/blog\/wp-json\/wp\/v2\/tags?post=11548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}