[Python-3000] default argument surprises
James Y Knight
foom at fuhm.net
Wed Aug 27 06:23:59 CEST 2008
On Aug 27, 2008, at 12:14 AM, Chris Monson wrote:
> Now thinking as a language designer, C# has a ?? operator where A??B
> is shorthand for (B if A is None else A) except you only have to
> write A once and A is only evaluated once. A Pythonesque version of
> this would be just "else":
> def foo(arg=None, arg2=None):
> arg = arg else bar
> arg2 = arg2 else list()
> And I think that metaphor is easy to read. Chains of else operators
> can be useful:
> x = f() else g() else h() else 0
> Not a bad idea. Looks like the time machine is at work again:
> x = f() or g() or h() or 0
Well...no. "else" here is significantly different from "or": it only
tests for None, not falseness.
>>> A = 0
>>> B = 1
>>> B if A is None else A # suggested equivalence for "A else B"
>>> A or B
Although, maybe you just meant that "else" and "or" have such similar
behavior, and their names do not obviously distinguish their behavior.
And, thus, people would find them confusing, so "else" should not be
added to the language. That I could agree with.
More information about the Python-3000