PYTHON MAGIC METHOD:
In Python, magic methods (also known as dunder methods) are special methods
that start and end with double underscores (e.g., .__init__’ ,’ __str__’,’
__add__’). These methods provide a way to define how objects of a class
behave in various contexts, such as arithmetic operations, comparisons, string
representations, and more.
![]() |
Python Dunder |
IT'S TYPES:
1. __init__(self, ...): This method is the constructor and is automatically called when an object is created. It initializes the object's attributes.
2. __str__(self): This method defines the string representation of an object. It's called by the built-in str() function and print().
3. __repr__(self): This method defines a more detailed and unambiguous string representation of an object. It's used by the built-in repr() function.
4. __len__(self): This method defines the behavior of the len() function when called on an object. It's commonly used for sequences (lists, strings, etc.).
5. __getitem__(self, key): This method allows you to define how indexing (obj[key]) works for objects of your class.
6. __setitem__(self, key, value): This method allows you to define how assignment to an index (obj[key] = value) works for objects of your class.
7. __delitem__(self, key): This method allows you to define how deletion of an index (del obj[key]) works for objects of your class.
8. __add__(self, other): This method defines the behavior of the + operator when used with objects of your class.
9. __sub__(self, other): This method defines the behavior of the - operator when used with objects of your class.
10. __eq__(self, other): This method defines the behavior of the equality (==) operator.
11. __lt__(self, other): This method defines the behavior of the less than (<) operator.
12. __gt__(self, other): This method defines the behavior of the greater than (>) operator.
13. __le__(self, other): This method defines the behavior of the less than or equal to (<=) operator.
14. __ge__(self, other): This method defines the behavior of the greater than or equal to (>=) operator.
15. __del__(self): This method is called when an object is about to be deleted. It can be used to perform cleanup operations.
THE '__INIT__ ' FUNCTION:
The ‘__init__ ‘function is a special method in Python
classes, often referred to as the constructor. It's automatically called when
you create an instance of a class and is used to initialize the attributes
(properties) of the object. The’ __init__ ‘method allows you to set up
the initial state of the object by providing values for its attributes.
EXAMPLE:
THE '__STR__' FUNCTION:
The’ __str__’ function is a special method in Python classes that
defines a human-readable string representation of an object. This method is
automatically called when you use the built-in ‘str()’ function or the ‘print()’
function with an object of your class. It allows you to customize the textual representation
of your objects in a way that is useful and informative.
EXAMPLE:
THE '__REPR__' FUNCTION:
The ‘__repr__’ function is a special method in Python
classes that defines an unambiguous string representation of an object. This
method is automatically called when you use the built-in ‘repr()’
function with an object of your class. It's commonly used for providing
detailed debugging information about an object, which can be especially useful
during development and testing.
EXAMPLE:
THE '__LEN__' FUNCTION:
The ‘__len__’ method is a special method in Python
classes that allows you to define the behavior of the built-in ‘len()’
function when used with objects of your class. By implementing the ‘__len__’
method, you can customize how the length of your objects is determined.
EXAMPLE:
THE '__ITER__' FUNCTION:
The ‘__iter__’ method is a special method in Python
classes that allows you to define how objects of your class should behave when
used in iteration contexts, such as loops or when using the’ iter()‘function.
By implementing the ‘__iter__’ method, you can create iterable objects
that can be looped over using a for loop or used with other
iteration-related functions.
0 Comments