Function within a function in python

You defined f2 in the local namespace of f1 only; it is not available globally.

If you want such a nested function to be available at the module level, you'd have to either return it from the function, or define a global variable to store it in:

def f1(a):
    def f2(x):
       return a+x
    return 2*a, f2

then call that as result, f2 = f1(somevariable_or_literal).

The global approach is not recommendable (using a global rarely is) but would look something like:

f2 = None

def f1(a):
    global f2
    def f2_local(x):
         return a+x
    f2 = f2_local
    return 2*a

at which point f2 will be set when you have called f1.


f2 is defined inside f1. Therefore, it's scope only extends inside the function f1. Outside of that space, the function f2 doesn't even exist, which is why you're getting the error.

If you were to call f2 from somewhere inside f1 after f2 is defined, it would work.

Short Description of Python Scoping Rules has a good explanation of how scope works in Python.

Tags:

Python