12.6. Operator Arithmetic About¶
Operator Overload
Operator Overload is the Pythonic way
Operator Overload allows readable syntax
Operator Overload allows simpler operations
All examples in this chapter uses
dataclasses
for you to focus on the important code, not boilerplate code just to make it works
12.6.1. SetUp¶
>>> from dataclasses import dataclass
12.6.2. Operators¶
Source: https://github.com/python/cpython/blob/main/Grammar/python.gram#L695
Comparison:
==
,!=
,<=
,<
,>=
,>
,not in
,in
,is not
,is
Bitwise:
|
,^
,&
,<<
,>>
Arithmetic:
+
,-
,*
,/
,//
,%
,@
,**
,~
12.6.3. Recap¶
>>> a = int(1)
>>> b = int(2)
>>> a + b
3
>>> a = float(1.0)
>>> b = float(2.0)
>>> a + b
3.0
>>> a = str('1')
>>> b = str('2')
>>> a + b
'12'
>>> a = list([1])
>>> b = list([2])
>>> a + b
[1, 2]
>>> a = tuple((1,))
>>> b = tuple((2,))
>>> a + b
(1, 2)
12.6.4. Problem¶
dataclass
is used to generate__init__()
and__repr__()
dataclass
does not have any influence on addition
>>> @dataclass
... class Vector:
... x: int
... y: int
>>>
>>>
>>> a = Vector(x=1, y=2)
>>> b = Vector(x=2, y=3)
>>> a + b
Traceback (most recent call last):
TypeError: unsupported operand type(s) for +: 'Vector' and 'Vector'
12.6.5. Solution¶
>>> @dataclass
... class Vector:
... x: int = 0
... y: int = 0
...
... def __add__(self, other):
... new_x = self.x + other.x
... new_y = self.y + other.y
... return Vector(new_x, new_y)
>>>
>>>
>>> a = Vector(x=1, y=2)
>>> b = Vector(x=2, y=3)
>>> a + b
Vector(x=3, y=5)