__init__ is the initialiser

Ethan Furman ethan at stoneleaf.us
Sat Feb 1 09:28:03 CET 2014

On 01/31/2014 09:51 PM, Steven D'Aprano wrote:
> On Sat, 01 Feb 2014 15:35:17 +1100, Chris Angelico wrote:
>> The two methods could have been done as a single method, __construct__,
>> in which you get passed a cls instead of a self, and you call
>> self=super().__construct__() and then initialize stuff.
> That would be called __new__ in Python. There's no *need* to use __init__
> for anything (except old-style classic classes in Python 2).

While there may not be a /need/ for two, having two is quite handy.  Having __new__ take care of the nuts and bolts (or 
foundation, as Terry put it), and being able to further customize with __init__ (where the kitchen goes, how many 
bedrooms, to follow along with Terry) is quite useful.  One of my favorite Enum recipes uses that pattern to have some 
basic behavior, with some other behavior that is easily overridable/extendable [1].


[1] http://stackoverflow.com/q/19330460/208880

More information about the Python-list mailing list