Object copying itself

Jason Orendorff jason at jorendorff.com
Fri Feb 15 19:48:27 EST 2002


Dale Strickland-Clark writes:
> I want object references copied (shallow copy) but the lists need to
> be copied entirely (deep copy).
>
> Do I have to do this by hand or is there a neat way of doing it?

Well, depending on what you need, one rather unwholesome way might be:

class MyClass:
    def __init__(self, other):
        self.__dict__ = copy.deepcopy(other.__dict__)

    def __copy__(self):
        return self

    def __deepcopy__(self):
        return self

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list