473,320 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

newb: Python Module and Class Scope

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
3 1257
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jon Moldover | last post by:
Hi, I'm using Python in my win32 app by linking to the python23.dll. I'm trying to expose some c++ code in my app to Python so I can make application calls from Python scripts (according to the...
5
by: Alexandre | last post by:
Hi, Im a newb to dev and python... my first sefl assigned mission was to read a pickled file containing a list with DB like data and convert this to MySQL... So i wrote my first module which...
23
by: Yannick Patois | last post by:
Hi, Under some naming conditions of module files, it seems that python lost class static variables values. It seems only to append when importing a "badly" named module that itself import a...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.