Possible Duplicate:
Python init and self what do they do?
Can anyone explain what __init__(self) does? I’ve been reading other questions and people mentioning super classes? What’s a super class? I thought procedures were supposed to start off with
def print_word(word):
print word
Is __init__(self) some sort of special case since it has underscores and “self”? sorry for the lack of specific terminology… still learning.
__init__(self)is a special function used within what are called “classes”. Classes let you define your own datatypes/object and this__init__function is called when ever you create a new instance of the datatype/object that you’ve defined. It’s like saying “hey, every time you make one of these things, make sure you run this code too”. In object oriented programming, we call this kind of function a “constructor”. It’s a function that’s used to “construct” a new object. Theselfis a reference to the object that’s actually being created. In other words, the object is technically already created by the time you get to your__init__function.__init__just gives you the opportunity to initializes various aspects of your object (hence the name__init__).Python is a mixture of both functional, procedural, and object oriented paradigms. This
__init__business has to do with objects. For more information on object oriented programming you can checkout the wikipedia article.