What do __init__ and self do in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Python, __init__ and self are fundamental concepts tied to the language's approach to object-oriented programming (OOP). Understanding both is crucial for effectively using Python's classes and objects.
Understanding __init__
The __init__ method in Python is what is known as a constructor in other object-oriented programming languages. It is the first method that’s called when an object is created from a class. The primary purpose of this method is to initialize the attributes of the object.
Example:
In this example, the Car class has an __init__ method with parameters make and model. When a Car object is created, these parameters are used to set the make and model attributes of the object.
Understanding self
self represents the instance of the class. By using the self keyword, we can access the attributes and methods of the class in within its methods. It binds the attributes with the given arguments.
Example:
In this example, self.make and self.model are used to refer to the attributes set by the __init__ method. The method display_info uses these attributes to return information about the car.
Why self?
In Python, self is explicitly used in method definitions and calls to differentiate between method calls of objects. Unlike languages like Java, where it is optional and implicit, Python requires that it be explicitly stated in both the method parameter and when calling an attribute/method through it.
Table: Summary of __init__ and self
| Term | Description | Usage |
__init__ | Special method used to initialize newly created objects from a class. | def __init__(self, <args>): |
self | Represents the instance of the class, allowing access to the class attributes and methods. | Used as the first parameter in class methods and to access properties/methods. |
Key Considerations
__init__can take any number of parameters and is typically used to initialize attributes of a new object.- Although
selfis a convention, and not a Python keyword, it is universally followed, and it’s recommended to adhere to it for consistency. - Python does not support method overloading, including for the
__init__method. However, you can achieve similar functionality by setting default parameter values or by using variable-length argument lists. - Always make sure
__init__either explicitly returnsNoneor has no return statement. Any other return value will lead to aTypeError.
Additional Subtopics
- Dynamic Attributes: Python allows for dynamic attribute creation, meaning attributes can be added to an object on the fly, which is not directly tied to class definitions.
- Private and Protected Members: Using
_and__prefixes, Python supports a form of encapsulation by convention (single underscore) and name mangling (double underscores). - Destructors: Python uses
__del__method which, unlike__init__, is called when an object is being destroyed. Understanding its usage with respect to__init__can be crucial for managing resources.
By understanding __init__ and self, programmers can leverage Python’s object-oriented capabilities effectively, making code more modular, reusable, and organized.
Related reading
- What do __init__ and self do in Python?
- What do and mean in Python?
- What do args and kwargs mean?
- What do double star/asterisk and star/asterisk mean in a function call?
- What do I use for a max-heap implementation in Python?
- What do lambda function closures capture?
- What do square brackets mean in pip install?
- What do the python file extensions, .pyc .pyd .pyo stand for?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.