Python Tutorials · Python __init__ Method
Python __init__ Method
Learn all about Python __init__ Method in this comprehensive tutorial.
5 min read advanced
- •All classes have a built-in method called __init__(), which is always executed when the class is being initiated.
- •Without the __init__() method, you would need to set properties manually for each object:
- •You can also set default values for parameters in the __init__() method:
- •The __init__() method can have as many parameters as you need:
The __init__() Method
All classes have a built-in method called __init__(), which is always executed when the class is being initiated.
The __init__() method is used to assign values to object properties, or to perform operations that are necessary when the object is being created.
python
Note: Note: The __init__() method is called automatically every time the class is being used to create a new object.
Why Use __init__()?
Without the __init__() method, you would need to set properties manually for each object:
python
Using __init__() makes it easier to create objects with initial values:
python
Default Values in __init__()
You can also set default values for parameters in the __init__() method:
python
Multiple Parameters
The __init__() method can have as many parameters as you need:
python
Module quiz
Test your knowledge on this module
5 questions