trouble with copy/deepcopy

Alexander Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org
Mon May 16 12:58:24 EDT 2005


Hello!

I have trouble with copy/deepcopy. It seems, I just don't understand something.
Please explain where things goes wrong and how to do it the right way.

I have one class:

class Distribution:
  __gr_on_transp=dict()
  __ostatok_m=dict()
and so on

And, I want to make full copy of it:
d1=Distribution()
d2=copy.deepcopy(d1)

But, members-dictionaries are don't copied. For example if I will write:
d1.clear()
which clears all two dictionaries, I will find that d2's dictionaries are also
empty!!!

Now I'am using the quick-hack:

class Distribution:
  __gr_on_transp=dict()
  __ostatok_m=dict()
  ...
  def my_copy(self):
    d2=copy.deepcopy(self)
    d2.__gr_on_transp=self.__gr_on_transp.copy()
    d2.__ostatok_m=self.__ostatok_m.copy()
    return d2

It's work well, but I don't understand why previous is wrong and how to do it
in right way.

Alexander, zatv at bk.ru



More information about the Python-list mailing list