In Python 3.x this is possible:

def f1():
        x = 5
        def f2():
                nonlocal x
                x+=1
        return f2

The problem and a solution to it, for Python 2.x as well, are given in this post. Additionally, please read PEP 3104 for more information on this subject.

Answer from hochl on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › inner functions cannot access outer variable if it is primitive, why exactly is it like this?
r/learnpython on Reddit: Inner functions cannot access outer variable if it is primitive, why exactly is it like this?
November 26, 2021 -

Take this function as an example:

def foo(self, param1):

count = 0
buffer = []

def inner(x):

buffer.append(x + param1)
count += 1

inner(5)

So, the inner function can freely access param1 and buffer. However, it will complain about count, and in order to make it work, you need to declare "nonlocal count".

I was wondering why is this the case, how is the stack organized such that param1 and buffer is accessible without the need to declare nonlocal, but for count, it needs a nonlocal declaration? I'm guessing it has something to do with it being a primitive... but then again, so is param1

Discussions

python - inner function accessing outer function's variable - Stack Overflow
Let's suppose to have this simple code: def my_outer_function(): outer_var = 123 def my_inner_function(): return outer_var + 1 return my_inner_function get_inner = my_outer_fu... More on stackoverflow.com
🌐 stackoverflow.com
January 4, 2017
python - Inner function accessing variables directly from outer function scope - What are the drawbacks? - Code Review Stack Exchange
I saw this snippet in a code that I am working on. And I feel that accessing the outer function variable directly in the inner function brings some undesirable magic. For e.g. when I read the funct... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
July 27, 2016
Nested function has an issue with a variable
I’m messing around with making some art with Turtle Graphics, and I made a nested function with a variable to create a shape when it’s called. I first tried to create a variable and assign it a value of 60 in the function, and also tried moving it out of the function, as it keeps giving ... More on discuss.python.org
🌐 discuss.python.org
4
0
June 21, 2023
ironpython - Is there a way to use variable from outer function in the inner function body, in Python? - Stack Overflow
Why I need it this way: A Python script that we run before has to become module now (the collection of methods). OuterFunction did not exist before and InnerFunction is just an example for a big number of very complex methods that bear an already existing logic. theVariable is just an example for multiple global variables ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-inner-functions
Python Inner Functions - GeeksforGeeks
May 22, 2026 - They are mainly used for: Encapsulation: Hiding helper logic from external access. Code Organization: Grouping related functionality for cleaner code. Access to Outer Variables: Inner functions can use variables of the enclosing (outer) function.
🌐
Real Python
realpython.com › ref › glossary › nested-scope
nested scope | Python Glossary – Real Python
This is possible because Python retains access to the outer scope for the inner function, even after the outer function has finished executing. By default, a nested scope works only for reading an enclosing variable, not for assigning to it.
🌐
Python Morsels
pythonmorsels.com › nested-functions
Nested functions in Python - Python Morsels
June 10, 2026 - When a function is defined within a function and the inner function accesses variables from the outer function, Python uses a "closure" to keep track of those variables that are defined just outside the inner function, in its enclosing scope.
🌐
Analytics Vidhya
analyticsvidhya.com › home › how nested functions are used in python?
How nested functions are used in Python? - Analytics Vidhya
March 4, 2025 - In programming languages that support this feature (like Python), the inner function can access variables and parameters from its enclosing (outer) function, even after the outer function has finished executing.
🌐
DEV Community
dev.to › divshekhar › python-inner-function-nested-functions-1dbn
Python Nested Function - DEV Community
May 23, 2023 - Python Inner Functions or Nested Functions can access the variables of the outer function as well as the global variables.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › accessing-python-function-variable-outside-the-function
Accessing Python Function Variable Outside the Function - GeeksforGeeks
July 23, 2025 - A closure allows a nested function to remember and access variables from its enclosing function even after execution. This method ensures encapsulation while avoiding global scope pollution.
🌐
Real Python
realpython.com › inner-functions-what-are-they-good-for
Python Inner Functions: What Are They Good For? – Real Python
2 weeks ago - To do that, you call inner_func() on the last line of outer_func(). This is the quickest way to write and use an inner function in Python. Inner functions provide several interesting possibilities beyond what you see in the example above.
🌐
Dive into Python
diveintopython.org › home › learn python programming › functions in python › nested functions
Nested Functions in Python - Powerful Tool for Organized Code
May 3, 2024 - To use nested functions in Python, define the outer function and then define the inner function within it. The inner function has access to the variables within the outer function, but not vice versa.
🌐
Python.org
discuss.python.org › python help
Nested function has an issue with a variable - Python Help - Discussions on Python.org
June 21, 2023 - I’m messing around with making some art with Turtle Graphics, and I made a nested function with a variable to create a shape when it’s called. I first tried to create a variable and assign it a value of 60 in the function, and also tried moving it out of the function, as it keeps giving me the error “defined in enclosing scope referenced before assignment” and I don’t know why.
🌐
Learn By Example
learnbyexample.org › python-variables-scope
Python Variables Scope - Learn By Example
April 20, 2020 - Here’s an example that tries to reassign enclosing (outer) function’s local variable inside a nested (inner) function. # enclosing function def f1(): x = 42 # nested function def f2(): x = 0 print(x) # x is 0 f2() print(x) # x is still 42 f1() Here, the value of existing variable x didn’t change. Because Python created a new local variable named x that shadows the variable in the outer scope.
🌐
Stack Abuse
stackabuse.com › python-nested-functions
Python Nested Functions
December 21, 2018 - The inner function is able to access the variables that have been defined within the scope of the outer function, but it cannot change them. There are a number of reasons as to why we may need to create an inner function. For instance, an inner function is protected from what happens outside it.
🌐
Medium
medium.com › devtechie › nested-functions-in-python-52d9b0938bb3
Nested functions in Python. This article takes on nested functions… | by DevTechie | DevTechie | Medium
November 22, 2023 - def outer_function(): # this is outer function or enclosing function def inner_function(): # this is inner or nested function print ("Hello from nested (inner) function.") # definition of inner function ends here print ("Hi from enclosing (outer) function.") # call the nested function inner_function()
🌐
Quora
quora.com › Why-does-Python-let-you-access-a-list-but-not-an-int-from-a-nested-inner-function-Python-function-scope-nested-development
Why does Python let you access a list, but not an int from a nested inner function (Python, function, scope, nested, development)? - Quora
Answer (1 of 2): You can always access the variables of the outer scope (assuming you haven't shadowed them), but you cannot modify them without first declaring them [code ]nonlocal[/code]. If you don't, an attempt in an inner scope to assign to a name used in the outer scope will only create a ...
🌐
Medium
martinxpn.medium.com › python-variable-scope-30-100-days-of-python-c7f74c46c0a5
Python Variable Scope (30/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - The nonlocal keyword in Python is used to indicate that a variable is in an outer, but not global, scope. This keyword allows you to modify a variable in an outer local scope within a nested function: def outer_function(): x = 10 # outer local ...