The __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.
  • A default_param and corresponding value can 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

Codebyte Example

The example below showcases the __init__() method being implicitly called after a new instance of the Home class, home, is created with rooms and stories attributes passed in:

Edit this page on GitHub


Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Python on Codecademy