473,698 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1268
johnny <ra*******@gmai l.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************ **********@q75g 2000hsh.googleg roups.com>,
johnny <ra*******@gmai l.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*******@moto rola.comwrote:
In article <11************ **********@q75g 2000hsh.googleg roups.com>,
johnny <ra*******@gmai l.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
2013
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 Python windows FAQ I shouldn't have to make a seperate dll, see link below). I create a MyModule.i file for swig which contains a simple test c++ class. I run swig on it with the following options: swig -python -c++ -includeall -shadow MyModule.i,...
5
2033
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 reads this pickled file and writes an XML file with list of tables and fields (... next step will the module who creates the tables according to details found in the XML file). If anyone has some minutes to spare, suggestions and comments would be...
23
2149
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 module with a static variable in it (looks complex, but see the example below, pretty simple): First a working example: <<<< file: aTest.py
75
4646
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 functions are simplified versions of the full featured methods for compiled regular expressions. Most non-trivial applications always use the compiled form UNQUOTE
162
10244
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.