PEP 308: Pep Update
John La Rooy
nospampls.jlr at doctor.com
Thu Feb 13 20:41:22 EST 2003
On Thu, 13 Feb 2003 00:00:38 -0800
David Eppstein <eppstein at ics.uci.edu> wrote:
> The more this discussion drags on, the less I like any of the proposals.
> The current "and or" trick is looking less good to me, too, but I'm
> starting to think it's better to place more effort on programmer
> education about why it's bad than on ways to replace it with something
> less broken.
>
Lets see. The "and or" trick saves me about 4(#1) lines of code at the expense
of hiding it's intention from a casual reader.
But at least "and" and "or" have other uses.
It seems silly to me(#2) to add a whole syntactical construct (and maybe a keyword)
to the language just to save 4 lines every now and then.
The cond(C,x,y) function some people have suggested makes sense if you're doing a
bunch of these, so long as you don't need short circuiting.
If you need short circuiting - just use 4 lines. Chances are you're going to need
extra comments to explain what you're doing and now you probably have extra space
at the ends of the lines.
if C:
temporary_variable=f(x)
else:
temporary_variable=g(y)
do_something_with(temporary_variable)
If you need a bunch of short circuiting ternary operators meaning you'll save
lots of lines. Use
cond(C, lambda:f(x) , lambda:g(y) )()
and have a comment explaining what it does.
Could we have cond() as a standard function with True and False? Then we could
take the "and or" trick out of the FAQ and tell people "use cond()" instead.
If they ask for a short circuiting form, tell a white lie :o)
John
#1 Has anyone got an example where more than 4 lines would be saved by a
ternary operator?
#2 This is my opinion. You are allowed one too.
More information about the Python-list
mailing list