Skip to content Skip to sidebar Skip to footer

Can We Access Inner Function Outside Its Scope Of Outer Function In Python Using Outer Function?

Just for the sake of curiosity I wanna know this.. I know scope of inner function is limited to outer function body only, but still is there any way so that we can access the inner

Solution 1:

>>>defmain():...defsub():...        a=5...print a...>>>main.__code__.co_consts
(None, <code object sub at 0x2111ad0, file "<stdin>", line 2>)
>>>exec main.__code__.co_consts[1]
5

Solution 2:

You can if you return the inner function as a value

>>>defmain():...defsub():...        a = 5...print a...return sub...>>>inner = main()>>>inner()
5

or you can attach it to main as a property (functions are objects after all):

>>>defmain():...defsub():...        a = 5...print a...    main.mysub = sub...>>>main()>>>main.mysub()
5

but you better document your very good reason for doing this, since it will almost certainly surprise anyone reading your code :-)

Solution 3:

No, you can't. The inner function is not an attribute of the outer function.

The inner function only exists after its def statement is executed (while the outer function is executed), and it stops to exist when the function exits.

You could return the inner function, of course.

Solution 4:

A function is just another object in Python and can be introspected.

You can get the outer function body at runtime and parse/eval it to make the function available in the current namespace.

>>> import inspect
>>> defouter():
        definner():
            print"hello!">>> inspect.getsourcelines(outer)
([u'def outer():\n', u'    def inner():\n', u'        print "hello!"\n'], 1)

Not really the same thing as calling outer.inner(), but if you are not making the inner function explicitly available outside the scope of the outer function, I guess it is the the only possibility.

For example, a very naive eval attempt could be:

>>>exec('\n'.join([ line[4:] for line in inspect.getsourcelines(outer)[0][1:] ]))>>>inner()
hello!

Solution 5:

An inner function is just a local variable like any other so the same rules apply. If you want to access it you have to return it.

Post a Comment for "Can We Access Inner Function Outside Its Scope Of Outer Function In Python Using Outer Function?"