New to Python: Anyone know what __init__(self) does? (simple expl. please!)

init, python, self

Solution

`__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. The `self` is 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.

Problem

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.

Original source

Related problems