PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Container(object):
def __init__(self, *items):
self._items = list(items)
return
def __add__(self, other):
self._items = [int(i)+other for i in self._items]
return self
# -----EDIT------
def __radd__(self, other):
"""
Reverse Addition, useful to inform Python interpreter how to handle addition between a built in type and a user built class
Thanks @Russ for showing this
Magic Method
"""
self._items = [int(i)+other for i in self._items]
return self
def __str__(self):
return str(self._items)
def __repr__(self):
return str(self)
con = Container(1, 2, 3, 4, '5')
print(con + 1)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run