[Python-Dev] Re: Trinary Operators
Andrew Koenig
ark@research.att.com
06 Feb 2003 16:29:14 -0500
Guido> I'm not sure yet how lambda and if/else should group relative
Guido> to each other.
Note first that there is no question of precedence between "if" and
"else" because every "if" is paired with exactly one "else".
The problem, then, arises in the following contexts:
... lambda ... : ... if ... else ....
... if ... else ... lambda ... : ...
There doesn't appear to be a problem with the lambda on the right,
either! I say that because once you get past the colon, you're in
an ordinary expression and whatever rules applied before still apply.
So I think the only real problem is with the lambda on the left.
There, I think that the if/else should bind more tightly than the
lambda, because it will be much more common to have a conditional as
the body of a lambda expression than it will be to have a conditional
that yields a lambda. I think of this as the canonical example:
fact = lambda n: n*fact(n-1) if n>0 else 1
It's hard to think of an equally nice one with lambda on the right,
but maybe this is semi-convincing:
future_x = x if callable(x) else lambda: x
It doesn't bother me that the relative precedence of if/else and
lambda is different on the left and right, because lambda is
one of these oddball low-precedence unary operators in the first place.
C++ has similar oddities with the relative precedence of :? and = .
--
Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark