trouble with copy/deepcopy

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon May 16 16:18:17 EDT 2005


In <MSGID_2=3A5025=2F3.131_42890b9a at fidonet.org>, Alexander Zatvornitskiy
wrote:

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

Those two dicts are *class variables* not *instance variables*!

> 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!!!

That clears only one dictionary at class level.  Which is visible on both
instances.

class Distribution:
    def __init__(self):
        self.__gr_on_transp = dict()
        self.__ostatok_m = dict()
    ...

This creates *instance variables* which should be deepcopied without
problems.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list