7.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

7.6.1. SetUp

>>> from dataclasses import dataclass

7.6.2. Operators

7.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)

7.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'

7.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)

7.6.6. Further Reading