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