newb: Python Module and Class Scope
Dave Baum
Dave.Baum at motorola.com
Thu May 10 10:56:03 EDT 2007
In article <1178805769.658287.178240 at q75g2000hsh.googlegroups.com>,
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():
>
Yes, class A can access main. The name "main" will be defined at the
top level of the module, and is considered a global for that module.
Code within that module can access it simply as "main". Code in other
modules would have to import the module in order to use symbols within
it. For example...
### myModule.py ####
class A:
def m():
main()
def main():
pass
### other.py ###
import myModule
def main():
pass
class B:
def o():
main() # calls main() in this module
myModule.main() # calls main in myModule
Dave
More information about the Python-list
mailing list