__init__ function problem

Carl Banks pavlovevidence at gmail.com
Tue Nov 7 15:03:23 EST 2006


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__.)


Carl Banks




More information about the Python-list mailing list