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 function variable outside its scope or call the inner function outside its scope ?
In [7]: def main():
...: def sub():
...: a=5
...: print a
...:
In [8]: main()
In [9]: main.sub()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/dubizzle/webapps/django/dubizzle/<ipython-input-9-3920726955bd> in <module>()
----> 1 main.sub()
AttributeError: 'function' object has no attribute 'sub'
In [10]:
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.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.
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:
You can if you return the inner function as a value
or you can attach it to main as a property (functions are objects after all):
but you better document your very good reason for doing this, since it will almost certainly surprise anyone reading your code :-)
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.