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

Martin P. Hellwig martin.hellwig at dcuktec.org
Thu Mar 25 19:29:09 EDT 2010


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



More information about the Python-list mailing list