Copy constructors

Andrew Dalke dalke at acm.org
Fri Aug 10 09:35:00 CEST 2001


Joal Heagney wrote:
>And adding onto that an automatic copy of the instance's __dict__ -->

>... def __copy__(self):
>... class Temp: pass
>... newbie = Temp()
>... newbie.__class__ = self.__class__
>... newbie.__dict__ = copy.deepcopy(self.__dict__)
>... print "very little work"
>... return newbie

Or

  def __copy__(self):
      return loads(dumps(self))

(but this assumes the created string is small enough to be
negligible.)

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list