trouble with copy/deepcopy
Mike Meyer
mwm at mired.org
Tue May 17 17:38:14 EDT 2005
Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org (Alexander Zatvornitskiy) writes:
> ðÒÉ×ÅÔ Marc!
>
> 16 ÍÁÑ 2005 × 22:18, Marc 'BlackJack' Rintsch × Ó×ÏÅÍ ÐÉÓØÍÅ Ë All ÐÉÓÁÌ:
>
> MR> That clears only one dictionary at class level. Which is visible on
> MR> both instances.
>
> MR> class Distribution:
> MR> def __init__(self):
> MR> self.__gr_on_transp = dict()
> MR> self.__ostatok_m = dict()
> MR> ...
>
> MR> This creates *instance variables* which should be deepcopied
> MR> without problems.
> Hmm. I don't find definition of "class variable" in manual. I try to test it on
> such a simple test and find that such variable is NOT shared between copies:
Actually, it is shared - but only for reference. If you assign to it,
you'll create an instance variable of the same name. As Peter
explained, if you remove the instance variable, the class variable
becomes visible again. Try this:
py> class C:
... q = []
...
py> c1 = C()
py> c2 = C()
py> c3 = C()
py> c1.q.append(1)
py> c2.q.append(2)
py> c3.q.append(3)
py> c1.q, c2.q, c3.q
([1, 2, 3], [1, 2, 3], [1, 2, 3])
py>
<mike
--
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list
mailing list