-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnexpectedBugs.py
More file actions
28 lines (25 loc) · 1022 Bytes
/
UnexpectedBugs.py
File metadata and controls
28 lines (25 loc) · 1022 Bytes
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
# ---------------------------------------------------------------------------------------------------------------------------------
# Incorrect method, creates a bug with growing list each call that wasn't passed in same memory location
def add_items(item, my_list=[]):
print(f"{id(my_list)=}")
my_list.append(item)
return my_list
# Bug occurs here
print(add_items(1))
print(add_items(2))
print(add_items(3))
# This works since we're not using default list
print(add_items(1, [10, 20]))
print(add_items(2, [99, 100]))
print(add_items(3, []))
# ---------------------------------------------------------------------------------------------------------------------------------
# The fix is to set the default value to "None"
def add_item(item, my_list=None):
if my_list is None:
my_list = [] # If None, Create a fresh list with each call
print(f"{id(my_list)=}")
my_list.append(item)
return my_list
print(add_item(1))
print(add_item(2))
print(add_item(3))