def append_to(arr=[])
	arr.append(1)
	return arr
 
 
print(append_to())
# [1]
 
print(append_to())
# Expectation: [1]
# Reality: [1, 1]

At first glance, this seems counter-intuitive. When I am calling the append_to function again, shouldn’t the argument arr be set to its default value []?

This is a problem with mutable defaults in Python and it isn’t inherently a design flaw. This happens because functions defined are actually objects and mutable values carry over. When a function is defined, it is defined only once and the default argument inside the definition is evaluated only once when the function is defined

Workaround

# Set None as the placeholder default
def append_to(arr=None)
	if arr is None:
		arr = []
		
	arr.append(1)
	return arr
 
 
print(append_to())
# [1]
 
print(append_to())
# [1]