__new__ to create copy

Uwe Mayer merkosh at hadiko.de
Fri Feb 6 05:21:37 EST 2004


Hi,

I want to create a copy of an object from out of its base class:

class A(object):
  def copy(self):
...

class B(A):
...

>>> b = B()
>>> b.copy()

I'm not sure how to do this:

def copy(self):
    cpy = self.__new__(self.__class__)
    return cpy

seems not to call the constructor __init__().

How is such a thing done correctly? Where is the exact difference between
__new__ and __init__?


Thanks
Uwe



More information about the Python-list mailing list