Accessing an instance of a class from within instance of another class

Accessing an instance of a class from within instance of another class

Post by Paul Kl » Sat, 01 May 2010 23:17:38


Am 30.04.2010 13:05, schrieb Stefan Krastanov:

[snipp]

Why do you think the data is copied? Both viewers will hold a reference
to the same data object:

Type "help", "copyright", "credits" or "license" for more information.
>>> class d(object):
... a = 1
... b = 2
... c = 3
...
>>> d1 = d()
>>> d1.a
1
>>> class view1(object):
... def __init__(self, data):
... self.data = data
... def data(self):
... return self.data
...
>>> class view2(object):
... def __init__(self, data):
... self.data = data
... def data(self):
... return self.data
...
>>> v1 = view1(d1)
>>> v2 = view2(d1)
>>> v2.data.b
2
>>> v2.data.b = 4
>>> v2.data.b
4
>>> v1.data.b
4
>>>

hth
Paul