Initializing Objects
Explore the world of initializers and learn why they are necessary for a class.
We'll cover the following
What is an initializer?
As the name suggests, the initializer is used to initialize an object of a class. It’s a special method that outlines the steps that are performed when an object of a class is created in the program. It’s used to define and assign values to instance variables.
The initialization method is similar to other methods but has a pre-defined name, __init__
.
The double underscores mean this is a special method that the Python interpreter will treat as a special case.
The initializer is a special method because it does not have a return type. The first parameter of __init__
is self
, which is a way to refer to the object being initialized.
It is always a good practice to define it as the first member method in the class definition.
Defining initializers
Initializers are called when an object of a class is created. See the example below:
Get hands-on with 1200+ tech skills courses.