A decorator cannot, and does not "retrieve" a function. A decorator is a function which takes a function (the decorated function), and returns another function (the result of the decorating process).
If the wrapped function were to return its inner function, then the function created by the decorator could do anything it liked with that.
Given a function object, can I retrieve a function defined within that object by name?
No, because a def
statement is an executable statement. You are confused by the static appearance of the function text. Until the outer function is invoked, there is no inner function object for you to access. This is necessarily so, as the inner function object closes over the variables (context) of the invocation of the outer function. Or to put it more simply, in the following code, the two functions returned1
and returned2
are different functions:
def outer(x):
def inner(y):
return x+y
return inner
returned1 = outer(2)
returned2 = outer(2)