You cannot, not unless fib returns inner somehow. inner is essentially a local variable inside the scope of fib and you can't access a function's locals from outside of it. (That wouldn't even make sense, since the locals don't exist except when the function is running. Think about it -- would it make sense to access fib's c variable from outside of the function?)

Answer from cdhowie on Stack Overflow
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ nested-functions-in-python
Nested Functions in Python
January 6, 2020 - These are the conditions you need to create a closure in Python: ... Closures make it possible to pass data to inner functions without first passing them to outer functions with parameters like the greeting example at the beginning of the article. They also make it possible to invoke the inner function from outside of the encapsulating outer function.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-do-nested-functions-work-in-Python
How do nested functions work in Python?
September 28, 2023 - It is well-understood how to access a global variable within a function, but what about accessing the variable of an outside function? The following program demonstrates the scope of the nested functions โˆ’ ยท # creating an outer function def outerFunc(): sample_str = 'Hello tutorialspoint python' # creating an inner function def innerFunc(): print(sample_str) # calling an inner function inside the outer function innerFunc() # calling outer function outerFunc() ... It is clear from the above example that it is equivalent to accessing a global variable from a function.
Discussions

python - Accessing a function within a function(nested function?) - Stack Overflow
Python noob here. How do I get hold of the 'inner' function within the 'fib' function? from time import sleep class Fibonacci(object): def __init__(self, a, b, limit=50): self.a = a ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
[Python] | How to call nested functions
I don't think you can. Why not simply define f2 as a global function? More on reddit.com
๐ŸŒ r/learnprogramming
5
1
October 17, 2021
Call nested function from another function in python? - Stack Overflow
I have a function inside inside another and a third function. How can I call my nested function inside of my third function? Is there any special libraries I can use? I am not allowed to edit a() o... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Call Nested Function in Python - Stack Overflow
Now the reason i have this is that ... from the function and the arguements. You can't therefore just put the decorator on getThis as the request variable is unique-ish and the cache is useless. So i created the inner function which has repeatable args (search_term). However to invalidate the cache (ie refresh), the invalidation function requires scope to know of the 'getData' function so also needs to be nested. Therefore i need to call the nested ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
DEV Community
dev.to โ€บ divshekhar โ€บ python-inner-function-nested-functions-1dbn
Python Nested Function - DEV Community
May 23, 2023 - This feature in Python helps us to encapsulate the function. If you try to call the inner function from the outside scope of outer function.
๐ŸŒ
|
artifas.com โ€บ home โ€บ 2021 โ€บ june โ€บ 14 โ€บ python call inner function from outside
python call inner function from outside
June 14, 2021 - Then Python will first look if ... If you try to call any of the inner function from outside their scope, NameError is raised during function call....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-inner-functions
Python Inner Functions - GeeksforGeeks
May 22, 2026 - Example 2: This example shows how ... creating a new local copy. ... Explanation: nonlocal tells Python to use the variable a from the outer scope instead of creating a new local one....
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [python] | how to call nested functions
r/learnprogramming on Reddit: [Python] | How to call nested functions
October 17, 2021 -

This is the code I am trying to run:

x = 1
def f1():
    x = 2

    def f2():
        global x
        x = 3

print(x)
f1()
print(x)
f2()
print(x)

Output:

1
1
Traceback (most recent call last):
    f2()
NameError: name 'f2' is not defined

What I meant to do was call the f2() function but then it showed an error.

My question is how do I call the f2() function outside the nested block?

๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ how to call a nested function: python nested function call!
Nested Function Call : How to call a Nested Function in Python
October 16, 2020 - You can pass functions as an argument, return them or you can save them in data structures also. You can do it using Python Closures i.e. a function instance enclosed within an enclosing scope.
Top answer
1 of 7
33

I assume do_this and do_that are actually dependent on some argument of foo, since otherwise you could just move them out of foo and call them directly.

I suggest reworking the whole thing as a class. Something like this:

class Foo(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def do_this(self):
        pass

    def do_that(self):
        pass

    def __call__(self):
        self.do_this()
        self.do_that()

foo = Foo(x, y)
foo()
foo.do_this()
2 of 7
20

These previous answers, telling you that you can not do this, are of course wrong. This is python, you can do almost anything you want using some magic code magic.

We can take the first constant out of foo's function code, this will be the do_this function. We can then use this code to create a new function with it.

see https://docs.python.org/2/library/new.html for more info on new and https://docs.python.org/2/library/inspect.html for more info on how to get to internal code.

Warning: it's not because you CAN do this that you SHOULD do this, rethinking the way you have your functions structured is the way to go, but if you want a quick and dirty hack that will probably break in the future, here you go:

import new
myfoo = new.function(foo.func_code.co_consts[1],{}) 
myfoo(x,y) # hooray we have a new function that does what I want

UPDATE: in python3 you can use the types module with foo.__code__:

import types
myfoo = types.FunctionType(foo.__code__.co_consts[1], {})
myfoo()  # behaves like it is do_this()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: do_this() missing 2 required positional arguments: 'x' and 'y'
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ python-nested-functions
Python Nested Functions
December 21, 2018 - If the outer function is not called, the inner function will never execute. To demonstrate this, modify the above code to the following and run it: def function1(): # outer function print ("Hello from outer function") def function2(): # inner function print ("Hello from inner function") function2()
๐ŸŒ
Linux Hint
linuxhint.com โ€บ nested-functions-python
Linux Hint โ€“ Linux Hint
September 1, 2021 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
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.
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ curriculum help
Accessing inner functions in a class - Curriculum Help - The freeCodeCamp Forum
February 4, 2022 - Hi there! I am a beginner in python so I like to experiment. I was trying object oriented programming when I got stuck on this problem. Here is my code. The problem is in the last line of code. How can I access a nested inner function using object of a class?
๐ŸŒ
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 ... # call the nested function inner_function() Calling the inner function outside the body of the outer function will raise an error....