Python ClassesLesson 39Free preview
Python __init__ Method
This lesson is part of Python · All levels
Summary
The __init__ method is a constructor that is automatically executed whenever an object is created from a class. The main function is to initialize the instance attribute receiving the value given when calling a class. With __init__, each object is directly filled with initial data so that it is ready to use without separate manual settings.
Key points
- __init__ is executed automatically when the object is created, with self as the first parameter.
- The argument given when calling a class is accepted by the __init__ parameter other than self.
- The entrance self.x = x stores the value to the instance attribute so that it can be used by other methods.
- Special lines of reading methods such as __init__ are called dunder or magic methods.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.