What is __init__ in python?

Last updated 3 weeks, 6 days ago | 30 views 75     5

Tags:- Python

Python | __init__  constructor method

__init__ is a constructor method in Python and is automatically called to allocate memory when a new object/instance is created. All classes have a __init__ method associated with them.

 
class Student:
    
     # constructor
     def __init__(self, name, roll): 
          self.name = name
          self.roll = roll
     
     def display(self):
          print("Name:", self.name)
          print("Roll:", self.roll)