×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

What __init__ and self do in Python?

Last Updated : April 20, 2025

What are __init__ and self in Python?

The __init__ and self are two keywords in Python, which performs a vital role in the application.

To begin with, it is important to understand the concept of class and object.

Class

In Object-oriented programming, a class is a blueprint for creating objects of a particular data structure, provisioning the initial values for the state, and implementation of a behavior.

The user-defined objects are created using the class keyword.

Object

It is a basic unit of Object-Oriented Programming and each object is an instance of a particular class or subclass with class's methods or procedures and data variables.

With the above understanding,

Python __init__

__init__ is a reserved method in python classes. It is used to create an object of a class, something like a constructor in Java. This method when called creates an object of the class and it allows the class to initialize the attributes of the class.

Example usage of Python __init__

# A Sample class with init method 
class Country:     
    # init method or constructor 
    def __init__(self, name): 
        self.name = name 
    
    # Sample Method 
    def hello(self): 
        print('Hello, my name is', self.name) 
    
c = Country('India') 
c.hello()

Output

Hello, my name is India

In the above example, the line c = Country('India') invokes the method __init__ and creates an object c, which can then further invoke the method hello().

Python self

The word self is used to represent the instance of the class. Using self, the attributes and the methods of the class can be accessed.

Example usage of Python self

class Country:     
    # init method or constructor 
    def __init__(self, name): 
        self.name = name 
    
    # Sample Method 
    def hello(self): 
        print('Hello, my name is', self.name) 

Output

No output

In the above example, name is the attribute of the class Country and it can be accessed by using the self keyword.

Python `__init__` and `self` Exercise

Select the correct option to complete each statement about `__init__` and `self` in Python classes.

  1. The ___ method is a special method in Python that is called when an object is created from a class.
  2. The ___ parameter refers to the instance of the class in Python.
  3. The ___ method is automatically called to initialize the object’s attributes after it is created.
  4. The ___ keyword is used to refer to the current instance of the class within its methods.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.