Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 30, 2022 01:44 am GMT

Understanding `__init__` Method in Python

Table of Contents

  • What is Object-Oriented Programming?
  • What is __init__ method?
  • Example usecase of __init__ method

ko-fi

Whenever we have object oriented programming in Python, we mostly come across __init__ method which we usually dont 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 objects attributes.
The __init__ method lets the class initialize the objects 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}')

Now, we will create an object of the class.

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

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()

The output should be:

Title: Harry Potter and the Sorcerer StoneAuthor: JK. RowlingLanguage: English

Thanks for reading!

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

ko-fi


Original Link: https://dev.to/cycool29/understanding-init-method-in-python-3fko

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To