More assignment/copy novice confusion
Arthur Siegel
ajs at ix.netcom.com
Sun Dec 2 15:37:46 EST 2001
Laurent Pointal <laurent.pointal at l...>
>"Arthur Siegel" <ajs at i...> wrote in
>news:mailman.1007306883.27068.python-list at p...:
>> Given a class:
>>
>>>>> class p:
>> def __init__(self,a):
>> self.a=a
> But -
>
>>>> m=p([1,2,3])
>>>> r=p([])
>Here r and m reference two **different** objects of class p, with their own
>attributes.
>Try "r is m", and it will reply 0.
>>>> r.a=m.a
>Here m.a and r.a, which are attributes of different objects, reference the
>same list.
>>>> m.a=[1,2,4]
>And now m.a reference another list.
So indeed if I do m.a[2]=4 instead ofm.a =[1,2,4] then>>>> m.a
> [1, 2, 4]
>>>> r.a
> [1, 2, 4]All of this is far from obvious, but of course doesin the end make perfect sense.Art
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011202/7e600416/attachment.html>
More information about the Python-list
mailing list