Semi-newbie, rolling my own __deepcopy__
Steven Bethard
steven.bethard at gmail.com
Wed Apr 6 14:28:42 EDT 2005
Michael Spencer wrote:
> BTW, as I mentioned in a previous comment, I believe this would be more
> plainly written as type(self).__new__(), to emphasize that you are
> constructing the object without initializing it. (There is a
> explanation of __new__'s behaviour at
> http://www.python.org/2.2/descrintro.html#__new__).
There is also now documentation in the standard location:
http://docs.python.org/ref/customization.html
And just to clarify Michael's point here, writing this as __new__ means
that __init__ is not called twice:
py> class C(object):
... def __new__(cls):
... print '__new__'
... return super(C, cls).__new__(cls)
... def __init__(self):
... print '__init__'
...
py> c = C()
__new__
__init__
py> c2 = type(c)(); c2.__init__()
__new__
__init__
__init__
py> c3 = type(c).__new__(C); c3.__init__()
__new__
__init__
But definitely check the docs for more information on __new__. Some of
the interworkings are kind of subtle.
STeVe
More information about the Python-list
mailing list