A Few Other Magic Methods
Let’s discuss about few other magic methods:
- __len__ method
- __repr__ method
- __contains__ method
Overloading __len__ method
The len() method invokes the __len__ magic method. It takes one positional argument and returns the length of the object. Let’s see the below code:
Python3
class RectangleClass( object ): def __init__( self , area, breadth): self ._area = area self ._breadth = breadth def __len__( self ): return int ( self ._area / self ._breadth) rc = RectangleClass( 90 , 5 ) print ( len (rc)) |
Output
18
Importance of __repr__ method
The __repr__ magic method helps to represent an object in Python interactive terminal. It takes one positional argument – self. Let’s have a look, how an object is represented in Python interactive terminal without overloading the __repr__ method.
Python3
class RectangleClass( object ): def __init__( self , area, breadth): self ._area = area self ._breadth = breadth def __len__( self ): return int ( self ._area / self ._breadth) ## use python interactive terminal to check object representation. RectangleClass( 90 , 5 ) |
Output
<__main__.RectangleClass object at 0x7f9ecaae9710>
We can see, it returns the address of the object in the memory, which is not that useful. Let’s look into how we can overload the __repr__ method to return a useful object representation.
Python3
class RectangleClass( object ): def __init__( self , area, breadth): self ._area = area self ._breadth = breadth def __len__( self ): return int ( self ._area / self ._breadth) def __repr__( self ): """object representation""" return 'RectangleClass(area =% d, breadth =% d)' % \ ( self ._area, self ._breadth) RectangleClass( 90 , 5 ) RectangleClass( 80 , 4 ) |
Output
RectangleClass(area=90, breadth=5) RectangleClass(area=80, breadth=4)
__contains__ magic method
The __contains__ method is called when ‘in’ expression executes. It takes two positional arguments – self and item – and returns true if the item is present or otherwise, it returns false. Let’s examine through an example:
Python3
import datetime class DateClass( object ): def __init__( self , startDate, endDate): self .startDate = startDate self .endDate = endDate def __contains__( self , item): """ check whether a date is between the given range and return true or false""" return self .startDate < = item < = self .endDate dtObj = DateClass(datetime.date( 2019 , 1 , 1 ), datetime.date( 2021 , 12 , 31 )) result = datetime.date( 2020 , 6 , 4 ) in dtObj print ( "Whether (2020, 6, 4) is within the mentioned date range? " , result) result = datetime.date( 2022 , 8 , 2 ) in dtObj print ( "Whether (2022, 8, 2) is within the mentioned date range? " , result) |
Output
Whether (2020, 6, 4) is within the mentioned date range? True Whether (2022, 8, 2) is within the mentioned date range? False
Summary Hence, we can conclude that magic methods are a consistent data model to customize class behavior and enhance readability without losing their inherited feature. However, before giving a customized feature, make sure that whether customization is necessary or not.
Customize your Python class with Magic or Dunder methods
The magic methods ensure a consistent data model that retains the inherited feature of the built-in class while providing customized class behavior. These methods can enrich the class design and can enhance the readability of the language. So, in this article, we will see how to make use of the magic methods, how it works, and the available magic methods in Python. Let’s go through each of the sections:
- Magic Method Syntax
- Common Magic Methods
- Magic Methods For Binary Operators
- Magic Methods For Unary Operators
- A Few Other Magic Methods