Don't understand behavior; instance form a class in another class'instance

Discussion in 'Python' started by Martin P. Hellwig, Mar 25, 2010.

  1. Hi all,

    When I run the following snippet (drastically simplified, to just show
    what I mean):
    >>

    import platform, sys

    class One(object):
    def __init__(self):
    self.one = True

    def change(self):
    self.one = False

    class Two(object):
    def __init__(self):
    self._instance_one = One()
    self.one = self._instance_one.one
    self.change = self._instance_one.change

    if __name__ == '__main__':
    print(sys.version)
    print(platform.machine())
    print(80*'#')
    TEST1 = One()
    print(TEST1.one)
    TEST1.change()
    print(TEST1.one)
    TEST1 = None
    print(80*'#')
    TEST2 = Two()
    print(TEST2.one)
    TEST2.change()
    print(TEST2.one
    >>


    I get the following result:

    <<
    [GCC 4.2.1 20070719 [FreeBSD]]
    amd64
    ################################################################################
    True
    False
    ################################################################################
    True
    True
    ################################################################################
    <<

    What I don't understand why in the second test, the last boolean is True
    instead of (what I expect) False.
    Could somebody enlighten me please as this has bitten me before and I am
    confused by this behavior.

    Thanks in advance

    --
    mph
    Martin P. Hellwig, Mar 25, 2010
    #1
    1. Advertising

  2. On 03/25/10 23:41, Christian Heimes wrote:
    > Martin P. Hellwig schrieb:
    >> What I don't understand why in the second test, the last boolean is True
    >> instead of (what I expect) False.
    >> Could somebody enlighten me please as this has bitten me before and I am
    >> confused by this behavior.

    >
    > Hint: TEST2.one is not a reference to TEST2.__instance_one.one. When you
    > alter TEST2.__instance_one.one you don't magically change TEST2.one,
    > too. Python doesn't have variables like C pointers. Python's copy by
    > object (or share by object) behavior can be understand as labels. The
    > label TEST2.one references the same object as TEST2.__instance_one.one
    > until you change where the label TEST2.__instance_one.one points to.
    >
    > Christian
    >


    Ah okay thanks for the explanation, Am I correct in thinking (please
    correct me if I mangle up the terminology and/or totally are in the
    wrong ballpark) that this is more or less because the label of the first
    class is to an object (boolean with value False)
    and the label of the second class does not cascade to the first label
    for looking something up but instead during assignment sees that it is a
    label to an object instead of the object itself thus copies the label
    content instead?

    I probably expected classes namespaces to behave in about the same way
    as lists and dictionaries do, don't know where I picked that up.

    Thanks again,

    --
    mph
    Martin P. Hellwig, Mar 26, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. camotito

    QuickSort . I don't understand this behavior

    camotito, Apr 24, 2006, in forum: C Programming
    Replies:
    2
    Views:
    274
    Michael Mair
    Apr 24, 2006
  2. Heck
    Replies:
    9
    Views:
    387
  3. Ben Thomas
    Replies:
    2
    Views:
    334
    Triple-DES
    Jun 20, 2008
  4. Don't understand state in WebControls class

    , Aug 24, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    95
    Teemu Keiski
    Aug 27, 2006
  5. Raj Singh
    Replies:
    2
    Views:
    186
    Rick DeNatale
    May 29, 2008
Loading...

Share This Page