Sobes.tech
Junior — Senior

Function behavior with a mutable default parameter

livecode

Task condition

Determine what output will be obtained from two consecutive calls to the function add_item_to_list – first with the argument 1, then with the argument 2. Note that the second parameter of the function has a default value, which is a mutable object (list).

def add_item_to_list(item, my_list=[]):
    my_list.append(item)
    return my_list

print(add_item_to_list(1))
print(add_item_to_list(2))
Function behavior with a mutable default parameter… - sobes.tech