On Wed, Feb 10, 2021 at 2:19 AM Antoon Pardon <antoon.par...@vub.be> wrote: > > Most of us know of the perils of mutable default values. So I came up with > the following proof of concept: > > def copy_defaults(f): > > def wrapper(*args): > return f(*newargs) > return wrapper > > @copy_defaults > def prepare(value, lst = []): > for vl in range(value): > lst.append(vl) > return lst > > print(prepare(2)) > print(prepare(3)) > > Running the above will produce: > > [0, 1] > [0, 1, 2] >
Nice idea, but you've limited your functions to positional-only parameters. Extending this to support keyword arguments is a lot harder, and is why it's generally easier to use standard idioms like defaulting to None (or to a unique object) and then "if lst is None: lst = []" at the start of the function. Have you considered inspect.signature and its various methods? It may be useful here. ChrisA -- https://mail.python.org/mailman/listinfo/python-list