How can I avoid issues caused by Python’s early-bound default parameters (e.g. mutable default arguments “remembering” old data)?

def my_func(working_list=None):
    if working_list is None: 
        working_list = []

    # alternative:
    # working_list = [] if working_list is None else working_list

    working_list.append("a")
    print(working_list)

The docs say you should use None as the default and explicitly test for it in the body of the function.

Leave a Comment