Proposed PEP for a Conditional Expression
Tim Peters
tim.one at home.com
Mon Sep 10 01:29:47 EDT 2001
[Michael Chermside]
> Included in this email is a PEP which I am putting together dealing with
> the idea of introducing a conditional expression into Python.
Win, lose or draw, I want to thank you for writing this PEP! It's long
overdue, and it will be good to settle this issue.
> ...
> WORKAROUND_3: (this is the most common idiom)
> > x = (c and [a] or [b])[0]
> Here we create one-element lists and index into them.
> Since [a] is definitely NOT false (it's got 1 element),
> the and-or syntax will succeed.
This is frequently suggested, but almost never used. I invented this
"idiom" for Python in the very early 90's. It was in a joke thread, about
obfuscated Python. Anyone using it in real code should be shot; I've never
used it (which is why I'm still alive to type this <wink>).
> Reference Implementation
> ...
> The implementation is not expected to be difficult at all,
Beware: mucking with the parser isn't easy, especially as there are no
3-argument infix operators now. Code generation is easy in this case, but
fiddling the grammar may be a pit.
More information about the Python-list
mailing list