3 ms·
For lists, use a tuple as default, since it's immutable. def foo(allowed=()): truthy = ['true', 'True'] truthy += allowed # most things work
by sfink 3y ago
For lists, use a tuple as default, since it's immutable.
def foo(allowed=()):
truthy = ['true', 'True']
truthy += allowed # most things work
allowed.append('yes') # error
It's not perfect (`truthy = ['true', 'True'] + allowed` will not work).
- MawKKe 3y agowhile that works, it does not fully document what is expected from caller. And AFAIK using tuples like that is not very common. I'd prefer something like: def foo(allowed: Optional[List[ElemType]] = None): allowed = allowed or [] ...