PY-101 ยท Object-oriented Python
__init__ and attributes
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
__init__ is a special method that runs automatically when an object is created, and it sets up the object's attributes. Its first parameter, self, is the object being built, so self.name = name stores the name on that object. After that, obj.name reads it back.
- Write __init__
- Understand self
- Set and read attributes
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
class Student:def __init__(self, name, roll, marks=0):self.name = nameself.roll = rollself.marks = marksasha = Student("Asha", 12, 91)ravi = Student("Ravi", 7)print(asha.name, asha.roll, asha.marks)print(ravi.name, ravi.marks)ravi.marks = 78print("Updated:", ravi.marks)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
Asha 12 91
Ravi 0
Updated: 78 ๐ฏ Write class Product with __init__(self, name, price, qty=1). Create Product("Pen", 20, 3) and print its total value (price times qty): 60
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
def __init__(self, name, price, qty=1): self.name = name; self.price = price; self.qty = qty
- The problemCreating a student and then remembering to add name, roll number and marks one by one is error-prone. __init__ makes the class demand them up front.
- The Python idea
class Name:__init__ is a special method that runs automatically when an object is created, and it sets up the object's attributes. - In real lifeAPI client libraries take your key and settings in __init__: client = OpenAI(api_key=...), then every call uses self.api_key.
Start with a question
Creating a student and then remembering to add name, roll number and marks one by one is error-prone. __init__ makes the class demand them up front.
Almost every class you write or read starts with __init__. It defines what an object holds.
Explain it like I'm 10
self is the object saying "me". self.name = name means "write this name on MY label".
The technical explanation
def __init__(self, name, roll): runs when you call Student("Asha", 12).
Python passes the new object as self automatically; you pass only the other arguments.
Attributes can have defaults: def __init__(self, name, marks=None):.
Syntax
class Name:
def __init__(self, a, b=0):
self.a = a
self.b = b Real-world scenario
API client libraries take your key and settings in __init__: client = OpenAI(api_key=...), then every call uses self.api_key.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Methods โQuestions people ask
Is __init__ a constructor in Python?
It is usually called the constructor. Strictly, __new__ creates the object and __init__ initialises it, but you will almost always only write __init__.
Why is self needed in Python methods?
Python passes the object explicitly so methods know which object they are working on. The name self is a convention, but always use it.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner Is __init__ a constructor in Python?
It is usually called the constructor. Strictly, __new__ creates the object and __init__ initialises it, but you will almost always only write __init__.
beginner Why is self needed in Python methods?
Python passes the object explicitly so methods know which object they are working on. The name self is a convention, but always use it.
intermediate When does __init__ run?
When an object is created. Calling the class runs __init__.
intermediate What is self?
The object being created or used. self is the current object.
Quick revision
Remember
- Write __init__
- Understand self
- Set and read attributes
- Forgetting self. is the classic bug:
name = nameinside __init__ only creates a local variable that disappears.
Syntax
class Name:
def __init__(self, a, b=0):
self.a = a
self.b = b Official documentation
- Class objects and __init__ (docs.python.org)