Avoiding boilerplate by using immutable default arguments
Avoiding boilerplate by using immutable default arguments
vulwsztyn.codeberg.page
Avoiding boilerplate by using immutable default arguments in Python - Artur Mostowski
Hi, I recently realised one can use immutable default arguments to avoid a chain of:
python
def append_to(element, to=None): if to is None: to = []
at the beginning of each function with default argument for set, list, or dict.
This is the way you're supposed to write it in Python.
It is something you get used to, yet I think it's sad.
You can use mutable default arguments now with a new syntax:
https://peps.python.org/pep-0671/
Oh wow! This would be great I really hope it's accepted and implemented, makes a lot of sense!
This is only a Draft for now though
Thank you
Does not seems to work on 3.12:
Yeah, but you can also write it like I proposed which is less boilerplaty and gives the same advantages