top of page
Search

Exploring pythons magic methods

  • Writer: Nanditha Mahesh
    Nanditha Mahesh
  • May 17, 2024
  • 2 min read

Exploring Python's magic methods can be an exciting journey into the inner workings of the language. They're everything in object-oriented Python They're special methods that you can define to add magic to your classes Python that are surrounded by double underscores on both sides, like init() or add(). These methods allow classes to emulate built-in behavior and interact with Python operators or built-in functions.

What are the magic methods for comparison in Python?

Magic methods, also known as dunder methods (short for "double underscore"), are special methods in Python that are surrounded by double underscores on both sides, like init() or add(). In Python, comparison magic methods allow objects to define custom behavior when compared using comparison operators such as ==, <, >, <=, >=, and !=. These methods enable you to customize how instances of your class behave when compared to other objects. Here are the comparison magic methods:

1.      Initialization and Cleanup

2.      String Representation

3.      Attribute Access

4.      Container Methods

5.      Numeric Methods

6.      Comparison Metho

7.    Context Management 

Example are below:-

1.      eq__(self, other): Called when the equality operator == is used. It compares whether self is equal to other.

2.      _ne__(self, other): Called when the inequality operator != is used. It compares whether self is not equal to other.

3.      lt(self, other): Called when the less-than operator < is used. It compares whether self is less than other.

4.      le(self, other): Called when the less-than-or-equal-to operator <= is used. It compares whether self is less than or equal to other.

5.      gt(self, other): Called when the greater-than operator > is used. It compares whether self is greater than other.

6.      ge(self, other): Called when the greater-than-or-equal-to operator >= is used. It compares whether self is greater than or equal to other.

These are just a few examples; there are many more magic methods available for different purposes. Understanding and using these methods can greatly enhance the behavior and capabilities of your classes in Python.

When to use magic methods?

Magic methods should be used  to override a behavior on your object whenever you want to customize the behavior of your objects to interact with Python's built-in features or syntax. Here are some scenarios where magic methods are commonly used:

 

·       Cutomizing Object Behaviour

·       Operator Overloading

·       Cutomizing Attribute Access

·       Container Types

·       Context Managers

·       String Representation

 

What is an example of a magic function in Python?

For example, add() is a magic method that allows instances of the MyClass class to customize how they respond to the + operator. When obj1 + obj2 is called, Python internally invokes the add() method of obj1, passing obj2 as the other argument. Inside the add() method, we define the behavior for addition, which in this case simply adds the value attributes of obj1 and obj2 together.

 

 
 
 

Recent Posts

See All

Comments


© 2035 by Skyline

Powered and secured by Wix

bottom of page