__init__ function problem

Colin J. Williams cjw at sympatico.ca
Tue Nov 7 22:14:02 EST 2006


Carl Banks wrote:
> kelin,zzf818 at gmail.com wrote:
>> It says the __init__ is called immediately after an instance of the
>> class is created. What dose "immediately" mean?
>> And what is difference between the init method and the constructor in
>> Java?
> 
> For all intents and purposes, __init__ is a constructor.  It isn't
> technically, but you use it exactly the same way you use constructors
> in C++ and Java (well, acutally, Python __init__ is a quite bit more
> flexible, though also less convenient in many cases).  Don't worry that
> it isn't called a constructor.
> 
> In Python, the real constructor is called __new__, but you shouldn't
> use __new__ like C++ and Java constructors.  Usually there's no reason
> to use __new__ at all.  (The main use case is to return something other
> than a newly created object, such as a preallocated or cached object.
> For your normally functioning classes, you should use __init__.)
numpy.ndarray is an exception.  There, one must call __new__, or a 
factory function which does the same thing.

Colin W.
> 
> 
> Carl Banks
> 




More information about the Python-list mailing list