Hi, I recently realised one can use immutable default arguments to avoid a chain of:

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.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 4 points 1 year ago (7 children)

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.

  • source
  • hideshow 7 child comments
  • [โ€“] 5 points 1 year ago (5 children)

    You can use mutable default arguments now with a new syntax:

    https://peps.python.org/pep-0671/

    def bisect_right(a, x, lo=0, hi=>len(a), *, key=None):
    def connect(timeout=>default_timeout):
    def add_item(item, target=>[]):
    def format_time(fmt, time_t=>time.time()):
    
  • source
  • parent
  • hideshow 5 child comments
  • load more comments (4 replies)
  • load more comments (5 replies)