visibility between modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • max(01)*

    #1

    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 = anInstanceOfACl ass.att_1

    anInstanceOfACl ass = AClass()
    anInstanceOfAno therClass = AnotherClass()
    print anInstanceOfAno therClass.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."
    anInstanceOfACl ass = AClass()
    anInstanceOfACl ass.att2 = aGlobalString
    anInstanceOfAno therClass = AnotherClass()
    anInstanceOfAno therClass.att_1 = aGlobalString
    print anInstanceOfACl ass.att2 ### This should output "No way."
    print anInstanceOfAno therClass.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:

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

    ----

    any suggestion?

    bye max
  • Mike Meyer

    #2
    Re: visibility between modules

    "max(01)*" <max2@fisso.cas a> writes:
    [color=blue]
    > 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 = anInstanceOfACl ass.att_1
    >
    > anInstanceOfACl ass = AClass()
    > anInstanceOfAno therClass = AnotherClass()
    > print anInstanceOfAno therClass.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."
    > anInstanceOfACl ass = AClass()
    > anInstanceOfACl ass.att2 = aGlobalString
    > anInstanceOfAno therClass = AnotherClass()
    > anInstanceOfAno therClass.att_1 = aGlobalString
    > print anInstanceOfACl ass.att2 ### This should output "No way."
    > print anInstanceOfAno therClass.att_1 ### And this too[/color]


    Both these methods actually use globals. In ther first case, the
    global is "anInstanceOfAC lass", 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

    anInstanceOfACl ass = AClass()
    anInstanceOfAno therClass = AnotherClass(an InstanceOfAClas s)

    or maybe just:

    anInstaceOfAnot herClass = AnotherClass(AC lass())


    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    Working...