Section 1/4Meet self in __init__
Meet self in __init__
Explore this concept
Constructors & Instance Variables
Every time you create an object Python calls a special method called the constructor. In Python this is always named __init__. Inside it you set up the object's own data — called instance variables. The first parameter, self, is the new object being initialised.
Meet self in __init__
class Person:
def __init__(self, name, age):
self.name = name # store data on this object
self.age = age
p = Person("Alice", 30)
print(p.name) # → Alice
print(p.age) # → 30→
Alice
30__init__is called automatically when you writePerson("Alice", 30)selfis always the first parameter — Python passes the new object automaticallyself.name = namemeans "storenameon this Person object"- Without
self., the value would not be attached to the object