Class Methods in Python
What Is a Method?
In Python, a method is a function that is associated with an object and its class.
Methods are a key feature of object-oriented programming in Python, where data and functions are bundled together within a single entity known as an object, rather than being kept separate.
What's the Difference Between a Method and an Attribute?
A method is a function that is linked to an object, while an attribute is a piece of data that defines the characteristics of that object.
What Types of Methods Exist in Python?
Python includes several types of methods. Some are special (predefined) methods, while others can be custom-defined by the programmer.
Some of the most commonly used special methods in a class are:
- __init__
The constructor method. It is automatically invoked when an object of the class is instantiated. - __str__
This method converts an object into its string representation. - __del__
The destructor method. It is called to delete an object.