8.9. Operator Builtin
abs()
- will call method "abs" on objectx
(x.__abs__(y)
)bool()
- will call method "bool" on objectx
(x.__bool__(y)
)complex()
- will call method "complex" on objectx
(x.__complex__(y)
)dir()
- will call method "dir" on objectx
(x.__dir__(y)
)divmod()
- will call method "divmod" on objectx
(x.__divmod__(y)
)float()
- will call method "float" on objectx
(x.__float__(y)
)hash()
- will call method "hash" on objectx
(x.__hash__(y)
)hex()
- will call method "hex" on objectx
(x.__hex__(y)
)int()
- will call method "int" on objectx
(x.__int__(y)
)iter()
- will call method "iter" on objectx
(x.__iter__(y)
)len()
- will call method "len" on objectx
(x.__len__(y)
)next()
- will call method "next" on objectx
(x.__next__(y)
)oct()
- will call method "oct" on objectx
(x.__oct__(y)
)pow()
- will call method "pow" on objectx
(x.__pow__(y)
)reversed()
- will call method "reversed" on objectx
(x.__reversed__(y)
)round()
- will call method "round" on objectx
(x.__round__(y)
)
Function |
Method |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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