473,498 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

visibility between modules

hi.

if i have a single program file, different class instances can share
information in (at least) two fashions:

1. using instance variables:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfAClass.att_1

anInstanceOfAClass = AClass()
anInstanceOfAnotherClass = AnotherClass()
print anInstanceOfAnotherClass.att_1 ### This should print out 42

2. using globals:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
pass

aGlobalString = "No way."
anInstanceOfAClass = AClass()
anInstanceOfAClass.att2 = aGlobalString
anInstanceOfAnotherClass = AnotherClass()
anInstanceOfAnotherClass.att_1 = aGlobalString
print anInstanceOfAClass.att2 ### This should output "No way."
print anInstanceOfAnotherClass.att_1 ### And this too

----

i admit i prefer the fisrt way to do it. i have tried to make it work
even if the "main program" and each class definition reside in different
files, but i could not make it work:

ma**@172.17.1.201:/tmp$ cat AClass.py
class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"
ma**@172.17.1.201:/tmp$ cat AnotherClass.py
class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfAClass.att_1
ma**@172.17.1.201:/tmp$ cat Main.py
from AClass import *
from AnotherClass import *
anInstanceOfAClass = AClass()
anInstanceOfAnotherClass = AnotherClass()
print anInstanceOfAnotherClass.att_1 ### This should print out 42
ma**@172.17.1.201:/tmp$ python Main.py
Traceback (most recent call last):
File "Main.py", line 4, in ?
anInstanceOfAnotherClass = AnotherClass()
File "/tmp/AnotherClass.py", line 3, in __init__
self.att_1 = anInstanceOfAClass.att_1
NameError: global name 'anInstanceOfAClass' is not defined
ma**@172.17.1.201:/tmp$

----

any suggestion?

bye max
Jul 18 '05 #1
1 1340
"max(01)*" <ma**@fisso.casa> writes:
hi.

if i have a single program file, different class instances can share
information in (at least) two fashions:

1. using instance variables:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self):
self.att_1 = anInstanceOfAClass.att_1

anInstanceOfAClass = AClass()
anInstanceOfAnotherClass = AnotherClass()
print anInstanceOfAnotherClass.att_1 ### This should print out 42

2. using globals:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
pass

aGlobalString = "No way."
anInstanceOfAClass = AClass()
anInstanceOfAClass.att2 = aGlobalString
anInstanceOfAnotherClass = AnotherClass()
anInstanceOfAnotherClass.att_1 = aGlobalString
print anInstanceOfAClass.att2 ### This should output "No way."
print anInstanceOfAnotherClass.att_1 ### And this too

Both these methods actually use globals. In ther first case, the
global is "anInstanceOfAClass", that is bound to self.att_1 in the
__init__ method of AnotherClass.

The solution is to pass the instance in as a parameter:

class AClass:
def __init__(self):
self.att_1 = 42
self.att_2 = "Hello!"

class AnotherClass:
def __init__(self, instance):
sefl.att_1 = instance

anInstanceOfAClass = AClass()
anInstanceOfAnotherClass = AnotherClass(anInstanceOfAClass)

or maybe just:

anInstaceOfAnotherClass = AnotherClass(AClass())
<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2647
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
3
29660
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
3
1486
by: kele | last post by:
Do sub Namespaces have visibility to parent class. ie. I am trying to develop an application that is made up of modules and the main module contains a variables class that contains all the...
12
3866
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with...
4
5434
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
8
3355
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
0
876
by: Sudharshan S | last post by:
Hi all, I have been learning to write extension modules using the C API that python provides, and have hit a minor roadblock that is turning out to be major headache. My project essentially is...
7
1271
by: saneman | last post by:
I have made two modules: 1) main.cpp Contains a main function and struct P. Includes head.h and std::vector #include "head.h" #include<vector> #include<iostream> struct P {
3
1809
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
919
by: Terry Reedy | last post by:
Mohamed Yousef wrote: If you want to use module A in both B and C, B and C should both import A. No problem. ??
0
7126
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,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7381
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...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
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 ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.