8.9. Operator Builtin

  • abs() - will call method "abs" on object x (x.__abs__(y))

  • bool() - will call method "bool" on object x (x.__bool__(y))

  • complex() - will call method "complex" on object x (x.__complex__(y))

  • dir() - will call method "dir" on object x (x.__dir__(y))

  • divmod() - will call method "divmod" on object x (x.__divmod__(y))

  • float() - will call method "float" on object x (x.__float__(y))

  • hash() - will call method "hash" on object x (x.__hash__(y))

  • hex() - will call method "hex" on object x (x.__hex__(y))

  • int() - will call method "int" on object x (x.__int__(y))

  • iter() - will call method "iter" on object x (x.__iter__(y))

  • len() - will call method "len" on object x (x.__len__(y))

  • next() - will call method "next" on object x (x.__next__(y))

  • oct() - will call method "oct" on object x (x.__oct__(y))

  • pow() - will call method "pow" on object x (x.__pow__(y))

  • reversed() - will call method "reversed" on object x (x.__reversed__(y))

  • round() - will call method "round" on object x (x.__round__(y))

Table 8.7. Builtin Functions Overload

Function

Method

abs(obj)

obj.__abs__()

bool(obj)

obj.__bool__()

complex(obj)

obj.__complex__()

dir(obj)

obj.__dir__()

divmod(obj, other)

obj.__divmod__(other)

float(obj)

obj.__float__()

hash(obj)

obj.__hash__()

hex(obj)

obj.__hex__()

int(obj)

obj.__int__()

iter(obj)

obj.__iter__()

len(obj)

obj.__len__()

next(obj)

obj.__next__()

oct(obj)

obj.__oct__()

pow(obj)

obj.__pow__()

reversed(obj)

obj.__reversed__()

round(obj, ndigits)

obj.__round__(ndigits)

8.9.1. Syntax

>>> class MyClass:
...     def __abs__(self): ...                 # abs(a)
...     def __bool__(self): ...                # bool(a)
...     def __complex__(self): ...             # complex(a)
...     def __dir__(self): ...                 # dir(a)
...     def __divmod__(self, other): ...       # divmod(a)
...     def __float__(self): ...               # float(a)
...     def __hash__(self): ...                # hash(a)
...     def __hex__(self): ...                 # hex(a)
...     def __int__(self): ...                 # int(a)
...     def __iter__(self): ...                # iter(a)
...     def __len__(self): ...                 # len(a)
...     def __next__(self): ...                # next(a)
...     def __oct__(self): ...                 # oct(a)
...     def __pow__(self): ...                 # pow(a)
...     def __reversed__(self): ...            # reversed(a)
...     def __round__(self, ndigits): ...      # round(a)

8.9.2. Problem

>>> class Vector:
...     x: int
...     y: int
...
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f'Vector(x={self.x}, y={self.y})'
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> abs(a)
Traceback (most recent call last):
TypeError: bad operand type for abs(): 'Vector'

8.9.3. Solution

>>> class Vector:
...     x: int
...     y: int
...
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f'Vector(x={self.x}, y={self.y})'
...
...     def __abs__(self):
...         return (self.x**2 + self.y**2) ** 0.5
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> abs(a)
2.23606797749979

8.9.4. Use Case - 1

  • len()

>>> class Vector:
...     x: int
...     y: int
...
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f'Vector(x={self.x}, y={self.y})'
...
...     def __len__(self):
...         return 2
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> len(a)
2

8.9.5. Use Case - 2

  • float()

>>> class User:
...     pass
...
>>>
>>> a = User()
>>>
>>> float(a)
Traceback (most recent call last):
TypeError: float() argument must be a string or a real number, not 'User'
>>> class User:
...     def __float__(self):
...         return 13.37
...
>>>
>>> a = User()
>>>
>>> float(a)
13.37

8.9.6. Use Case - 3

  • abs()

>>> from math import sqrt
>>> from dataclasses import dataclass
>>>
>>>
>>> @dataclass
... class Vector:
...     x: int = 0
...     y: int = 0
...
...     def __abs__(self):
...         return sqrt(self.x**2 + self.y**2)
>>>
>>>
>>> abs(Vector(x=3, y=4))
5.0

8.9.7. Use Case - 1

  • round()

>>> pi = 3.1415
>>>
>>> type(pi)
<class 'float'>
>>>
>>> round(pi, 2)
3.14
>>>
>>> float.__round__(pi, 2)
3.14