newb: Python Module and Class Scope

Duncan Booth duncan.booth at invalid.invalid
Thu May 10 10:14:30 EDT 2007


johnny <rampeters at gmail.com> wrote:

> Can a class inside a module, access a method, outside of class, but
> inside of the module?
> 
> Eg.  Can instance of class a access main, if so how?  What is the
> scope of "def main()" interms of class A?
> 
> myModule:
> 
> class A:
>    main()
> 
> def main():
> 
> 
> thnx.
> 
> 

Yes;by using its name;global scope.

Why not try it for yourself?

N.B. Functions in Python do not exist until the def statement is executed, 
so the code like your sample will fail to find 'main' if you try to call it 
from inside the class body. A call from inside an instance method would be 
fine though (provided you didn't call it until main was defined), or a call 
from the class body would also be fine provided you define main before you 
define the class.



More information about the Python-list mailing list