-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
41 lines (28 loc) · 1.11 KB
/
Copy pathexample.py
File metadata and controls
41 lines (28 loc) · 1.11 KB
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
"""Mutable default argument — classic Python interview trap."""
def append_bad(item, target=[]):
target.append(item)
return target
def append_good(item, target=None):
if target is None:
target = []
target.append(item)
return target
def create_multipliers_wrong():
return [lambda x: i * x for i in range(4)]
def create_multipliers_good():
return [lambda x, i=i: i * x for i in range(4)]
if __name__ == "__main__":
print("=== Mutable default trap ===")
print(append_bad(1))
print(append_bad(2)) # surprise: [1, 2] not [2]!
print(f" shared default id: same list reused")
print("\n=== Fixed with None sentinel ===")
print(append_good(1))
print(append_good(2)) # [2] as expected
print("\n=== Same trap in class attributes ===")
print(" Use dataclass field(default_factory=list) or __init__ assignment")
print("\n=== Late binding in lambdas (related trap) ===")
wrong = create_multipliers_wrong()
good = create_multipliers_good()
print(f" wrong[0](10) = {wrong[0](10)} (expected 0, got 30)")
print(f" good[0](10) = {good[0](10)}")