7.8. Operator Increment
x += y
- will call method "iadd" on objectx
(x.__iadd__(y)
)x -= y
- will call method "isub" on objectx
(x.__isub__(y)
)x *= y
- will call method "imul" on objectx
(x.__imul__(y)
)x **= y
- will call method "ipow" on objectx
(x.__ipow__(y)
)x @= y
- will call method "imatmul" on objectx
(x.__imatmul__(y)
)x /= y
- will call method "itruediv" on objectx
(x.__itruediv__(y)
)x //= y
- will call method "ifloordiv" on objectx
(x.__ifloordiv__(y)
)x %= y
- will call method "imod" on objectx
(x.__imod__(y)
)
Operator |
Method |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7.8.1. Memory
tuple
is immutablelist
is mutabletuple += tuple
will generate newtuple
list += list
will update oldlist
__iadd__()
operator ontuple
is different than onlist
>>> a = [1, 2, 3]
>>> id(a)
4345884480
>>>
>>> a += [4, 5, 6]
>>> id(a)
4345884480
>>>
>>> a
[1, 2, 3, 4, 5, 6]
>>> a = (1, 2, 3)
>>> id(a)
4354672064
>>>
>>> a += (4, 5, 6)
>>> id(a)
4360031872
>>>
>>> a
(1, 2, 3, 4, 5, 6)
7.8.2. SetUp
>>> from dataclasses import dataclass, field
7.8.3. Syntax
>>> @dataclass
... class Vector:
... x: int
... y: int
...
... def __iadd__(self, other): ... # x += y calls x.__iadd__(y)
... def __isub__(self, other): ... # x -= y calls x.__isub__(y)
... def __imul__(self, other): ... # x *= y calls x.__imul__(y)
... def __ipow__(self, power, modulo=None): ... # x **= y calls x.__ipow__(y)
... def __imatmul__(self, other): ... # x @= y calls x.__imatmul__(y)
... def __itruediv__(self, other): ... # x /= y calls x.__itruediv__(y)
... def __ifloordiv__(self, other): ... # x //= y calls x.__ifloordiv__(y)
... def __imod__(self, other): ... # x %= y calls x.__imod__(y)
7.8.4. Example
>>> @dataclass
... class Vector:
... x: int
... y: int
...
... def __iadd__(self, other):
... self.x += other.x
... self.y += other.y
... return self
...
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> a += Vector(x=10, y=20)
>>> print(a)
Vector(x=11, y=22)
7.8.5. Add vs Iadd
>>> @dataclass
... class Vector:
... x: int
... y: int
...
... def __add__(self, other):
... return Vector(
... x = self.x + other.x,
... y = self.y + other.y)
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> id(a)
4435911632
>>>
>>> a += Vector(x=10, y=20)
>>> id(a)
4435972432
>>>
>>> print(a)
Vector(x=11, y=22)
>>> @dataclass
... class Vector:
... x: int
... y: int
...
... def __iadd__(self, other):
... self.x += other.x
... self.y += other.y
... return self
>>>
>>>
>>> a = Vector(x=1, y=2)
>>>
>>> id(a)
4437201808
>>>
>>> a += Vector(x=10, y=20)
>>> id(a)
4437201808
>>>
>>> print(a)
Vector(x=11, y=22)
7.8.6. Use Case - 0x01
>>> hero['gold'] += dragon['gold']
7.8.7. Use Case - 0x02
>>> class Group:
... def __init__(self):
... self.members = []
...
... def __iadd__(self, other):
... self.members.append(other)
... return self
>>>
>>>
>>> admins = Group()
>>> admins += 'mwatney'
>>> admins += 'mlewis'
>>> admins += 'rmartinez'
>>>
>>> admins.members
['mwatney', 'mlewis', 'rmartinez']
7.8.8. Assignments
"""
* Assignment: Operator Increment Add
* Complexity: easy
* Lines of code: 3 lines
* Time: 3 min
English:
1. Overload operator `+=`
2. Make `User` objects able to add `Groups`, for example:
a. `mark = User(firstname='Mark', lastname='Watney')`
b. `mark += Group(1, 'admins')`
c. `mark += Group(2, 'staff')`
3. Run doctests - all must succeed
Polish:
1. Przeciąż operator `+=`
2. Spraw aby do obiektów klasy `User` można dodać `Group`, przykład:
a. `mark = User(firstname='Mark', lastname='Watney')`
b. `mark += Group(1, 'admins')`
c. `mark += Group(2, 'staff')`
3. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `object.__iadd__() -> self`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> from pprint import pprint
>>> mark = User(firstname='Mark', lastname='Watney', groups=[])
>>> mark += Group(1, 'admins')
>>> mark += Group(2, 'staff')
>>> pprint(mark, width=50)
User(firstname='Mark',
lastname='Watney',
groups=[Group(gid=1, name='admins'),
Group(gid=2, name='staff')])
"""
from dataclasses import dataclass
@dataclass
class Group:
gid: int
name: str
# Make `User` objects able to add `Groups`, for example:
# a. `mark = User(firstname='Mark', lastname='Watney')`
# b. `mark += Group(1, 'admins')`
# c. `mark += Group(2, 'staff')`
# type: type[User]
@dataclass
class User:
firstname: str
lastname: str
groups: list[Group]