Python __init__ method

The __init__ method, if defined in a class, is called by Python whenever a class is instantiated. This method allows the class to be initialised and setup with whatever initial state is needed.

class MyClass: 
    def __init__(self):
        self.myValue = 42;

Here we are initialising the instance variable myValue with the value 42

The method allows you to define parameters, with the first parameter always being the self parameter, meaning the instance of the class being initialised. The __init__ method is never called directly by you, so to pass values to the parameters you defined, you pass the values to the class constructor like this:

class MyClass:    
    def __init__(self, value):
        self.myValue = value;

myObject = MyClass(42)

The value 42 will be passed to the __init__ function as the second parameter.