Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Class Constructor __init__ method

Python

python logo

In this tutorial we will learn about the class __init__ method in Python.

We learned about classes and how to create objects using classes in the Python - Classes and Objects tutorial. Feel free to check that out.

The __init__ method

The __init__ method is a special method of a class.

It is also called the constructor method and it is called when we create (instantiate) an object of the class.

We use the __init__ method to initialise class attributes or call class methods.

In the following Python program we are creating the __init__ method inside the Awesome class.

# class
class Awesome:

    # the init method
    def __init__(self):
        print("Hello from the __init__ method.")

# object of the class
obj = Awesome()

The above code will print the following output.

Hello from the __init__ method.

We get the above output because as soon as we create an object of the class the __init__ method is automatically called.

Points to note!

The first parameter of the __init__ method is always self.

The self refers to the object of the class and we use it to access methods and class attributes from inside the class.

Passing argument to the __init__ method

Like any other methods of a class we can also pass arguments to the __init__ method.

In the following Python program we are passing a string value to the __init__ method at the time of creating an object.

# class
class Awesome:

    # the init method
    def __init__(self, name):
        print("Hello from the __init__ method.")
        print("Name:", name)

# object of the class
obj = Awesome("Yusuf Shakeel")

The above code will give us the following output.

Hello from the __init__ method.
Name: Yusuf Shakeel

Calling other methods from the __init__ method

We can call other methods of the class from the __init__ method by using the self keyword.

# class
class Awesome:

    # the init method
    def __init__(self):
        print("Hello from the __init__ method.")

        # calling the class method
        self.greetings()

    # methods
    def greetings(self):
        print("Hello from the greetings() method.")

# object of the class
obj = Awesome()

The above code will print the following output.

Hello from the __init__ method.
Hello from the greetings() method.

Initialise class properties inside the __init__ method

We can create class properties that are specific to the class objects and initialise them inside the __init__ method.

In the following Python program we are creating Awesome class and initialising class property name inside the __init__ method.

# class
class Awesome:

    # class attribute common to all objects
    commonAttr = "Common Attribute"

    # the init method
    def __init__(self, name):

        # class attribute for the objects
        self.name = name

    # methods
    def greetings(self):
        print("Hello %s!" %self.name)

# object of the class
obj1 = Awesome("Tom")
obj2 = Awesome("Jerry")

# output
print("The common class attribute:")
print("obj1.commonAttr:", obj1.commonAttr)
print("obj2.commonAttr:", obj2.commonAttr)

print("Class attribute specific to object:")

print("obj1.greetings():")
obj1.greetings()

print("obj2.greetings():")
obj2.greetings()

The above code will print the following output.

The common class attribute:
obj1.commonAttr: Common Attribute
obj2.commonAttr: Common Attribute
Class attribute specific to object:
obj1.greetings():
Hello Tom!
obj2.greetings():
Hello Jerry!