__init__ function problem

kelin,zzf818@gmail.com zzf818 at gmail.com
Tue Nov 7 08:26:07 EST 2006


Hi,

Today I read the following sentences, but I can not understand what
does the __init__ method of a class do?
__init__ is called immediately after an instance of the class is
created. It would be tempting but incorrect to call this the
constructor of the class. It's tempting, because it looks like a
constructor (by convention, __init__ is the first method defined for
the class), acts like one (it's the first piece of code executed in a
newly created instance of the class), and even sounds like one ("init"
certainly suggests a constructor−ish nature). Incorrect, because the
object has already been constructed by the time __init__ is called, and
you already have a valid reference to the new instance of the class.
But __init__ is the closest thing you're going to get to a constructor
in Python, and it fills much the same role.

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?

Thanks a lot!




More information about the Python-list mailing list