Nearby lessons
85 of 108Python - __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 = marksImportant Points
selfmust be the first parameter- The method name must be
__init__ - It is optional, but very commonly used
Summary
| Term | Meaning |
|---|---|
| __init__ | Constructor method |
| self | Current object reference |
| Instance variables | Initialized in constructor |
🧠 Test Your Knowledge
3 QuestionsProgress: 0 / 3
Keep Going!Python - self Variable