Tsallaka zuwa abun ciki
Justice Tech School

The __init__ Method

__init__ runs automatically when you create an object. Use it to store the starting values in attributes with self. Its arguments are the ones you pass when you call the class.

Example
class Student:
    def __init__(self, name, level=1):
        self.name = name
        self.level = level

ada = Student("Ada")
tunde = Student("Tunde", 3)
print(ada.name, ada.level)
print(tunde.name, tunde.level)

Result

Ada 1
Tunde 3

Exercise

Name the setup method.

class A:
    def (self):
        pass

Quiz Test

When does __init__ run?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.