Beware of Python's mutable default arguments

In Python, using mutable default arguments in function definitions is considered an anti-pattern because it can lead to unintended behaviour. To showcase this let&rsquo;s write a simple function that appends a number to a list. Notice that my_list defaults to an empty list. def append_to_list ( number , my_list = []): my_list . append ( number ) return my_list list1 = append_to_list ( 1 ) print (…