__init__ (Python)
__init__ is the special method that runs automatically when you create a new object from a class. It is used to set up the object's initial attributes — like a constructor.
Python
The name is short for 'initialize' and has double underscores on each side, making it a 'dunder' method. Its first parameter, by convention named self, is the new object.
Examples
Python
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(2, 5)
print(p.x, p.y)Output:
2 5