DEV Community

Cover image for Understanding `__init__` Method in Python
cycool29
cycool29

Posted on • Updated on • Originally published at cycool29.github.io

Understanding `__init__` Method in Python


Table of Contents


ko-fi

Whenever we have object oriented programming in Python, we mostly come across __init__ method which we usually don’t fully understand.

Today, programmers are bound to come across Object-Oriented Programming (OOP) during their career. As a modern and popular programming language, Python provides all the means to implement the object-oriented philosophy. The __init__ method is at the core of Object-Oriented Programming and is one of the essential parts to create objects.

What is Object-Oriented Programming? 🧐

Before looking at __init__, it's very helpful if we have the idea of what is Object-Oriented Programming (OOP).

Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects.

An object is a collection of complex variables and functions and can be used to represent real entities like a button, an airplane, or a person.
To declare, initialize, and manipulate objects in Python, we use classes. They serve as templates from which objects are created.

Now the point comes... What is `__init__` method? 🤔

The __init__ method is a reseved method in Python classes. It is the Python equivalent of the C++ constructor in an object-oriented approach. When you create a new object of a class, Python automatically pass your arguments to the __init__ method and call it to initialize the object’s attributes.
The __init__ method lets the class initialize the object’s attributes and serves no other purpose. It is only used within classes.

Example usecase of `__init__` method 💡

Let's see how we can use __init__ method.

First, we create a Book class, with a simple __init__ method to initialize informations of the book and a function to print the book info.

class Book:
    def __init__(self, title, author, language):
        # Initialize book informations
        self.title = title
        self.author = author
        self.language = language
    def print_book_info(self):
        print(f'Title: {self.title}')
        print(f'Author: {self.author}')
        print(f'Language: {self.language}')
Enter fullscreen mode Exit fullscreen mode

Now, we will create an object of the class.

book1 = Book(title='Harry Potter and the Sorcerer Stone', author='JK. Rowling', language='English')
Enter fullscreen mode Exit fullscreen mode

When you create the object above, __init__ method was called and initialized the book infos.
To prove that, let's print the book info.

book1.print_book_info()
Enter fullscreen mode Exit fullscreen mode

The output should be:

Title: Harry Potter and the Sorcerer Stone
Author: JK. Rowling
Language: English
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! 😎


If you find this article helpful, consider buying me a coffee!

ko-fi

Top comments (0)