__new__ to create copy

Josiah Carlson jcarlson at nospam.uci.edu
Fri Feb 6 12:36:10 EST 2004


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

For some reason, I'm not finding the doc page, but I am pretty sure the 
below is the case.
If __new__ exists, it will be called.
If __new__ exists, it must call __init__ for __init__ to be called.
If __new__ doesn't exist, __init__ will be called, if it exists.


How I usually copy my classes:

class blah:
     def __init__(self, arg1, arg2, ...):
         self.arg1 = arg1
         self.arg2 = arg2
         ...
     def copy(self):
         return blah(self.arg1, self.arg2, ...)

It may not be pretty, but it works.

  - Josiah



More information about the Python-list mailing list