forked from mCodingLLC/VideosSampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plusequals_wat.py
70 lines (50 loc) · 1.46 KB
/
plusequals_wat.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class BadList(list):
def __add__(self, other):
print("running custom add")
return BadList(super(BadList, self).__add__(other))
def __iadd__(self, other):
print("running custom iadd")
return self + other
def plusequals_may_change_pointers():
x = 1
print(id(x))
x += 1
print(id(x), "changed")
x = []
print(id(x))
x += [1]
print(id(x), "not changed")
bad = BadList()
print(bad, "before append")
bad += [1, 2, 3] # manual extend
append_some_to_list(bad) # might do nothing?
print(bad, "after append")
def append_some_to_list(l):
l += [4, 5, 6]
def plusequals_meaning(x, y):
# x += y
result = x.__iadd__(y)
x = result
# x[0] += y
result = x[0].__iadd__(y) # calls __getitem__
x[0] = result # calls __setitem__
# x.val += y
result = x.val.__iadd__(y) # calls __getattr__
x.val = result # calls __setattr__
def tuple_what():
# a_tuple = (1, 2)
# a_tuple[0] += 1 # obvious error, tuple immutable
pros_and_cons = (["subscribing helps James", "subscribing feels good"],
["I have to click"])
pros = pros_and_cons[0]
pros += ["big numbers are cool"] # fine
try:
pros_and_cons[0] += ["James is cool"] # maybe?
except TypeError:
print("Error!")
print(pros_and_cons) # wat?
def main():
# tuple_what()
plusequals_may_change_pointers()
if __name__ == '__main__':
main()