Sobes.tech
Junior — Senior

Creating a separate instance without using a mutable default value

livecode

Task condition

It is necessary to write a function that returns a new object of the same type as the argument, but with an added element. The function should work correctly when the argument is not provided (default value — None), and each call should create an independent object.

def make_obj(data=None):
    if data is None:
        data = []
    data.append(1)
    return data

assert make_obj() == [1]
assert make_obj() == make_obj()
assert make_obj() == [1, 1, 1, 1]

The examples check that when no argument is given, a new list is created, not the same object reused between calls.