{
  "id": 16572394,
  "title": "Scope and namespaces in Python: where your variables live",
  "url": "https://urgent.news/2026/10/26/scope-and-namespaces-in-python-where-your-variables-live",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-10-26T07:43:49.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/fj_palacios/scope-and-namespaces-in-python-where-your-variables-live-1cjo"
  },
  "original_language": "en",
  "account": "Variables in Python are like residents of different neighborhoods, each with their own space and identity. When you define a variable inside a function, it lives in that function's local scope, like a resident who only exists within the four walls of their home. When the function finishes, the resident moves out, and the variable disappears. However, when you define a variable outside of any function, it lives in the module's global scope, like a permanent resident of the neighborhood.\n\nIf you try to modify a global variable from inside a function, Python assumes it's a new local variable and throws an error. To modify a global variable, you need to explicitly tell Python by using the global keyword. This is like sending a letter in advance to inform the postal service that a specific letterbox belongs to a particular resident.\n\nNamespaces are like internal dictionaries where Python stores names and their corresponding objects. Two variables with the same name in different scopes won't overwrite each other because they live in different namespaces. It's like having two folders with the same name in different directories - they are separate and distinct.\n\nThe search for a name in Python follows a specific order, LEGB (Local, Enclosing, Global, Built-in). Python starts looking for a name in the local scope of the current function, then moves outward through enclosing scopes, the module's global scope, and finally the built-in names. This is similar to how you might search for a missing item - you start by checking the immediate area and gradually expand your search outward.",
  "summary": "You have a variable called counter . You can see it. It's right there. You wrote it yourself, probably after a coffee that stopped being warm twenty minutes ago. You run the program and Python tells you it has no idea what variable you're talking about. Excuse me? Didn't we just see it? Did it leave the building? Do variables have private lives now? Welcome to scope : the part of Python that…",
  "key_points": [
    "Variables in Python have local scopes within functions and global scopes outside.",
    "Global variables require explicit declaration as global to avoid local variable creation.",
    "Namespaces store variables by scope, preventing overwriting in Python."
  ],
  "editors_take": "Understanding the LEGB hierarchy and namespace boundaries prevents accidental variable overwrites and ensures developers correctly manage data persistence across different functional layers in Python programs.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}