[Python-Dev] conditional expressions?
Anders J. Munch
andersjm at dancontrol.dk
Tue Oct 16 05:27:42 EDT 2001
"Tim Peters" <tim.one at home.com> wrote in message
news:mailman.1003177394.3900.python-list at python.org...
> Following is good clarification from Guido (on Python-Dev) about "the
rules" currently implemented by his patch.
[...]
> Some examples where no parentheses are needed (note that a comma binds
> less tight than a conditional expression -- same as for lambda):
>
> x = if 1 then 2 else 3, y
> f(if 1 then 2 else 3, y)
> a[if 1 then 2 else 3, y]
> `if 1 then 2 else 3`
> lambda: if 1 then 2 else 3
I see a problem with adding precedence rules. There doesn't seem to
be a natural precedence for the conditional expression operator. C
and C++ have been struggling with this for years; C and C++ have
different rules for this; compilers have warnings that say "Suggest
parentheses to clarify precedence". After 14 years of C and C++
programming I still couldn't tell you the precedence of ?: even if I
wanted to.
Quick, what does this snippet do if return_as_map is true?
return if return_as_map then {'A':a,'B':b} else a,b
Rather than open up a new venue of obfuscation possibilities in
Python, I'd go for mandatory parentheses around the conditional
expression.
- Anders
More information about the Python-list
mailing list