Magic Method

I came across an error with incaution when using the round() function:

def get_discount_price(ori_price, discount):
    discount_price = ori_price * (1 - discount)
    # forget to return `discount_price`
>>> round(get_discount_price(50, 0.2))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-abb2b34126d0> in <module>
----> 1 round(get_discount_price(50, 0.2))

TypeError: type NoneType doesn't define __round__ method

Reason

The Python built-in round() function is implemented by the __round()__ magic method (a.k.a. dunder methods). If weattempt to call round(x) or round(x, ndigits), Python will run the x.__round__() or x.__round__(ndigits) method, respectively.

Here I forgot to return discount_price in get_discount_price(). In other words, when I call get_discount_price(), it returns nothing, which is of NoneType. A NoneType object does NOT have or implement the __round()__ magic methods, which causes the TypeErroe.

Solution

When TypeError is raised during calling round() function:

  1. Check whether the object is of NoneType
  2. Check whether the __round()__ magic method is defined or implemented in the class of the object

Reference