reflection - Python: access nested functions by name -
inside function, can use dir() list of nested functions:
>>> def outer(): ... def inner(): pass ... print dir() ... >>> outer() ['inner']
...but what? how can access these functions name? see no __dict__
attribute.
first: sure want this? it's not idea. said,
def outer(): def inner(): pass locals()['inner']()
you can use locals
dictionary of local variable values, value of 'inner'
function. don't try edit local variable dict, though; won't work right.
if want access inner function outside outer function, you'll need store somehow. local variables don't become function attributes or that; they're discarded when function exits. can return function:
def outer(): def inner(): pass return inner nested_func = outer() nested_func()
Comments
Post a Comment