Nearby lessons

85 of 108

Python - __init__ Method

What Is __init__?

__init__ is the constructor method in Python classes.

It runs automatically when an object is created.

Purpose

Its main purpose is to initialize instance variables.

Example

class Student:
    def __init__(self, name, rollno, marks):
        self.name = name
        self.rollno = rollno
        self.marks = marks

Important Points

  • self must be the first parameter
  • The method name must be __init__
  • It is optional, but very commonly used

Summary

Term Meaning
__init__Constructor method
selfCurrent object reference
Instance variablesInitialized in constructor

🧠 Test Your Knowledge

3 Questions

Progress: 0 / 3
Keep Going!Python - self Variable