__init__() method initializes a newly created object. It is called each time a new instance of the defined class is created.
class ClassName: __init__(self, param1, param2, ..., paramN): self.param1 = param1 self.param2 = param2 self.paramN = paramN self.default_param = value
In a class definition, instance attributes can be set within the
__init__() in two ways:
- With named parameters (
param1, param2, ..., paramN) that will be required when a class instance is created.
valuecan also be defined so that every instance has access to this parameter.
Additionally, instance attributes can be added to a specific instance variable afterwards:
new_class_instance = ClassName(value1, value2, ..., valueN) new_class_instance.specific_variable = new_value
The example below showcases the
__init__() method being implicitly called after a new instance of the
home, is created with
stories attributes passed in: