3 ms·
For the first gotcha, using None as a default argument solves the problem, but checking `if not bar` instead of `if bar is None` can produce different results i
by michaelmior 13y ago
For the first gotcha, using None as a default argument solves the problem, but checking `if not bar` instead of `if bar is None` can produce different results if bar evaluates to None in a boolean context.
>>> def foo(bar=None):
... if not bar:
... bar = []
... bar.append("baz")
... return bar
...
>>> bar = []
>>> foo(bar)
["baz"]
>>> bar
[]
- icebraining 13y agoTrue, but foo shouldn't be modifying bar anyway; make a copy instead: bar = list(bar) if bar else []