How to explain self and __init__ in python so that beginners can understand?

Whenever we do object-oriented programming in Python, we mostly come across the __init__ method, which we usually don't fully understand.

Today, programmers are bound to encounter object-oriented programming (OOP) in their careers. As a modern and popular programming language, Python provides all the means to implement the object-oriented philosophy. The __init__ method is the core of object-oriented programming and is one of the basic parts of creating an object.

What is object orientation?

Before studying __init__, let's first understand what is Object-Oriented Programming (OOP), which will be very helpful to understand __init__.

Object-Oriented Programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure software programs into simple, reusable pieces of code blueprints (often called classes) that create a single instance of an object.

An object is a complex collection of variables and functions that can be used to represent real entities such as a button, an airplane, or a person.

To declare, initialize, and manipulate objects in Python, we use classes, which are templates for creating objects.

What is the __init__ method?

The __init__ method is an inescapable method in a Python class. It is equivalent to the C++ constructor in the object-oriented approach.

When you create a new object of a class, Python automatically passes your arguments to the __init__ method and calls it to initialize the object's properties.

The __init__ method lets the class initialize the properties of the object, it has no other effect, it is only used in the class.

__init__ usage example

Next, take a look at how to use the __init__ method.

First, we create a Book class, use a simple __init__ method to initialize the Book information, and use a function to print the Book information.

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}')

Now, we will create an object of a class.

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

When you create the above object, the __init__ method is called and the Book information is initialized.

To demonstrate this, let's print the Book information.

book1.print_book_info()

Below, take a look at the output:

Title: Harry Potter and the Sorcerer Stone
Author: JK. Rowling
Language: English

As can be seen from the output results, the parameters passed in when we initialized the Book object were initialized and assigned to the corresponding variables.


Hello, everyone, my name is Jackpop. I graduated from Harbin Institute of Technology with a master's degree. I have worked in big factories such as Huawei and Ali. If you have doubts about further education, employment, and technical improvement, you may wish to make friends:

I'm Jackpop, let's be friends!

Guess you like

Origin blog.csdn.net/jakpopc/article/details/124067983