Possible Duplicate:
Python's use of __new__ and __init__ ?
The way I understand it, __init__ is different from a constructor in Java, because __init__ only initializes an object that has already been constructed implicitly (because __init__ is called after __new__). However, everything that I have ever needed to define has used this latter property of a ‘constructor’ in Java. What would be a case in which a programmer would want to override __new__?
EDIT: For the record, I ask partly because I’m wondering what would be the advantage/disadvantage to overriding new vs. using a separate classmethod in the accepted answer to this question:
__new__actually happens before an object exists. It’s a static method of the type. Uses of__new__are when you want to control the creation of new objects, e.g. a singleton. If your__new__always returns the same instance of an object, it’s a singleton. You can’t do that with__init__.Generally, in “python as Guido intended it to be” you shouldn’t use
__new__more than once a month 🙂