By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

newb: Python Module and Class Scope

P: n/a
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.

May 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
johnny <ra*******@gmail.comwrote:
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.
May 10 '07 #2

P: n/a
In article <11**********************@q75g2000hsh.googlegroups .com>,
johnny <ra*******@gmail.comwrote:
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
May 11 '07 #3

P: n/a
Dave Baum <Da*******@motorola.comwrote:
In article <11**********************@q75g2000hsh.googlegroups .com>,
johnny <ra*******@gmail.comwrote:
>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.
No, the name "main" will be defined when the execution of the 'def main()'
statement is complete. It is not defined until that point.

In the example given the class statement is executed before the def
statement is executed so at that point main is still undefined.
May 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.