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

visibility between modules

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.