No, there is no language support for creating block scope.

The following constructs create scope:

  • module
  • class
  • function (incl. lambda)
  • generator expression
  • comprehensions (dict, set, list (in Python 3))
Answer from ThomasH on Stack Overflow
🌐
Python.org
discuss.python.org › ideas
Simplistic block scope - a syntactic sugar - Ideas - Discussions on Python.org
March 3, 2025 - Python lacks the block scope that exists in C-like languages and is marked there by curly braces {}. The common workaround is code like following: def _(): # ... _() In which case the contents of function _ are inside a separate block scope.
Discussions

Why does python lack nested scopes? (a la blocks in C, or scheme's "let" statements)

Prior to 2.1 or so, you couldn't even do that -- names were looked up in the function's local scope, in the module namespace, and then in the builtin function list, and that was all. So for instance a recursive inner function like this:

def f():
    def g(x):
        ...
        g(x-1)

would throw an exception due to not being able to look up 'g', as the local scope of f was never searched. There is still a wart in Python 2 when it comes to assigning to variables outside the local scope but not all the way up in the module; Python 3 adds the 'nonlocal' keyword which is similar to 'global' but for intermediate scopes.

In other words, it's a historical artifact.

In general I think the implication of this is that if you are using nested functions enough to keep running into scoping issues, the Pythonic thing to do would be to use some other construct, like a class or module, to organize that part of your code.

More on reddit.com
🌐 r/Python
64
67
June 12, 2014
Package to simulate block scopes in Python

A function also creates a scope and is usually the easier and cleaner way. What is the advantage of the scoping package?

More on reddit.com
🌐 r/Python
16
205
June 13, 2021
create block scope in Python
There's no way to create block scope in Python, but it's not needed. More on reddit.com
🌐 r/learnpython
18
1
September 24, 2019
Python Block Scopes - Someone explain this - Stack Overflow
I ran this block of code and the output was surprising. I know that I am reusing i, my question is why doesn't the loop exit after the first iteration since i is reassigned! Code: for i in range... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Kansas State University
textbooks.cs.ksu.edu › intro-python › 04-conditionals › 13-blocks-scope
Blocks & Scope :: Introduction to Python
July 30, 2026 - For now, you can think of everything as being part of the “Global” function in Python. We’ll revisit this concept later when we introduce functions. Let’s go back to the previous example, and look at that in terms of function scope · # Global block x = int(input("Enter a number: ")) if x > 5: # block A y = int(input("Enter a number: ")) if y > 10: # block B z = 10 else: # block C z = 5 elif x < 0: # block D a = -5 else: # block E b = 0 print("?")
🌐
Reddit
reddit.com › r/python › why does python lack nested scopes? (a la blocks in c, or scheme's "let" statements)
r/Python on Reddit: Why does python lack nested scopes? (a la blocks in C, or scheme's "let" statements)
June 12, 2014 -

In python, there is no non-ugly way to create a nested scope inside a function other than defining another function and then calling it.

I have to assume this is intentional. Is this python's way of hinting that functions should be short enough that you don't desire nested scopes?

Top answer
1 of 5
29

Prior to 2.1 or so, you couldn't even do that -- names were looked up in the function's local scope, in the module namespace, and then in the builtin function list, and that was all. So for instance a recursive inner function like this:

def f():
    def g(x):
        ...
        g(x-1)

would throw an exception due to not being able to look up 'g', as the local scope of f was never searched. There is still a wart in Python 2 when it comes to assigning to variables outside the local scope but not all the way up in the module; Python 3 adds the 'nonlocal' keyword which is similar to 'global' but for intermediate scopes.

In other words, it's a historical artifact.

In general I think the implication of this is that if you are using nested functions enough to keep running into scoping issues, the Pythonic thing to do would be to use some other construct, like a class or module, to organize that part of your code.

2 of 5
15

I would imagine it's due in part to three things:

  1. Python's "we're all adults here" attitude. Just like there's no form of access modifiers on classes or methods to prevent access to private data, there's no way to define your own scope since it effectively creates "private local variables" within that scope.

  2. The only time I've needed to explicitly create my own scope in C# is in switch/case blocks where I tried to define the same variable name within two of the case blocks. Python doesn't have separate declaration from initialization, so you wouldn't run into issues just setting the variable twice.

  3. In most cases, reusing the same variable name in such a way that requires custom scopes is probably a bad design.

🌐
Real Python
realpython.com › python-scope-legb-rule
Python Scope and the LEGB Rule: Resolving Names in Your Code – Real Python
July 16, 2026 - The names in your programs take on the scope of the code block in which you define them. When you can access a name from somewhere in your code, then the name is in scope. If you can’t access the name, then the name is out of scope. ... Because Python is a dynamically-typed language, its variables come into existence when you first assign them a value.
🌐
W3Schools
w3schools.com › python › python_scope.asp
Python Scope
A variable created in the main body of the Python code is a global variable and belongs to the global scope.
🌐
zdimension
zdimension.fr › block-scoping-in-python
Block scoping in Python | zdimension
November 26, 2024 - Python doesn't have block scoping. Or does it? ... A variable declared anywhere outside a function is in the (module’s) global scope, and a variable declared inside a function is in that function’s scope except exceptions in except blocks ...
Find elsewhere
🌐
Python
docs.python.org › 3.3 › reference › executionmodel.html
4. Execution model — Python 3.3.7 documentation
September 19, 2017 - If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name.
🌐
GitHub
github.com › l74d › scoping
GitHub - l74d/scoping: Probably the best solution to Python block scoping · GitHub
This is a package, as it says on the tin, to emulate block scoping in Python, the lack of which being a clever design choice yet sometimes a trouble.
Author: l74d
🌐
Reddit
reddit.com › r/python › package to simulate block scopes in python
r/Python on Reddit: Package to simulate block scopes in Python
June 13, 2021 -

Python's scoping rule is cleverly different from what we see in most programming languages, with which we can code things like

if flag:
    result = 1
else:
    result = 2

without initialization. But sometimes you would still miss the mainstream behavior.

In addition to readability and code organization (where your mileage may vary), block scoping in particular helps to have variables garbage collected as soon as possible, which is useful for situations where variables may refer to expensive resources (e.g. GPU arrays).

from scoping import scoping
a = 2
with scoping():
    assert(2 == a)
    a = 3
    b = 4
    scoping.keep('b')
    assert(3 == a)
assert(2 == a)
assert(4 == b)

https://github.com/l74d/scoping

https://pypi.org/project/scoping/

🌐
DataCamp
datacamp.com › tutorial › scope-of-variables-python
Python Variable Scope And The LEGB Rule Explained | DataCamp
September 11, 2025 - These mistakes account for most scope-related errors I see in practice. UnboundLocalError from assigning in a function: Python treats a name as local if it’s assigned anywhere in the function. Either move the read after the assignment, rename, or add global/nonlocal as appropriate. Expecting block scope: Names assigned in if/for/while/with persist in the containing scope.
🌐
Python documentation
docs.python.org › 3 › reference › executionmodel.html
4. Execution model — Python 3.14.8 documentation
Each occurrence of a name in the program text refers to the binding of that name established by the following name resolution rules. A scope defines the visibility of a name within a block.
🌐
Quora
quora.com › Why-doesnt-Python-have-block-scope-That-is-what-was-the-reason-for-that-design-decision
Why doesn't Python have block scope? That is, what was the reason for that design decision? - Quora
Answer (1 of 2): Why? Because it was badly designed from the beginning, and changing the way it worked would break existing code. No one can foresee the results of every decision, but defaulting to global scope is a classic mistake that has been made over and over again in Computer Language hist...
🌐
Readthedocs
python-textbok.readthedocs.io › en › 1.0 › Variables_and_Scope.html
Variables and scope — Object-Oriented Programming in Python 1 documentation
a is a local variable in the scope of my_function because it is an argument name. b is also a local variable inside my_function, because it is assigned a value inside my_function. c and d are both global variables. It doesn’t matter that d is created inside an if block, because the inside of an if block is not a new scope – everything inside the block is part of the same scope as the outside (in this case the global scope).
🌐
Hacker News
news.ycombinator.com › item
Python doesn't have block scope. This can lead to some surprising behaviour: x =... | Hacker News
February 10, 2021 - x = -1 for x in range(7): if x == 6: print(x, ': for x inside loop') print(x, ': x in outer') This outputs: · 6 : for x inside loop 6 : x in outer For consistency, I think it makes sense for match to behave in a similar way
🌐
Tutorial Teacher
tutorialsteacher.com › python › local-and-global-variables-in-python
Variable Scope in Python
In general, a variable that is defined in a block is available in that block only. It is not accessible outside the block. Such a variable is called a local variable. Formal argument identifiers also behave as local variables.