3 ms·
"stuff = stuff or []" This idiom is baked into the perl community. It's kind of funny to see a Pythonista deciding it's a good idea. (You can tell it hurts hi
by neutronicus 16y ago
"stuff = stuff or []"
This idiom is baked into the perl community. It's kind of funny to see a Pythonista deciding it's a good idea. (You can tell it hurts him too).
- jacobolus 16y agoIt’s not a good idea in Python. There are legitimate reasons to pass falsy objects as function arguments.
- robinhouston 16y agoFunnily enough, Perl (since version 5.10) actually has a solution to this. The expression $foo // "default" evaluates to "default" if $foo is undefined, and to $foo otherwise (even if it is defined but false).
- dagw 16y agoI've been programming python for on and off 12 years, and full time for the past 3, and this is the first time I've seen anyone recommending that idiom. So on the whole the python community does not think it's a good idea.
- deleted 16y ago[deleted]
- code_duck 16y agoJavaScript, too: stuff = stuff||[] since JavaScript doesn't even support default argument values.