[Tutor] Class and methods?
Sean Perry
shaleh at speakeasy.net
Wed Mar 30 18:53:41 CEST 2005
Kevin wrote:
> In a class is every def called a method and the def __init__(self) is
> called the constructor method? This class stuff is a little confusing.
> I don't have a problem writting a def I am still not clear on how to
> use a constructor. Is there a site that explains the constructor in
> great detail?
>
a method is simply a function attached to an object which acts on that
object. Nothing fancy.
You can think of __init__ as a constructor. I have seen Python people
refer to it as an initializer as well.
All __init__ is doing is preparing the object for use. Give initial
values to any variables, setup any datastructures, and/or maybe call a
few other functions / classes.
thing = MyClass(a,b)
the call to MyClass above gets mapped to MyClass.__init__(a,b).
Logically the flow is:
Python makes an instance of the object (MyClass)
Python calls __init__ if it is defined
when __init__ returns Python assigns the object to the waiting variable
More information about the Tutor
mailing list