Multiple conditional expression

John Machin sjmachin at lexicon.net
Mon Mar 2 16:19:21 CET 2009


On Feb 27, 7:55 pm, bearophileH... at lycos.com wrote:
> Chris Rebert:
>
> > That seems to just be an overly complicated way of writing:
>
> > spaces = bool(form.has_key('spaces') and form.getvalue('spaces') == 1)
>
> Better:
>
> spaces = bool(('spaces' in form) and form.getvalue('spaces') == 1)

Huh?? Much better is:
spaces = 'spaces' in form and form.getvalue('spaces') == 1

Then you go looking at "form": form.getvalue behaves just like
dict.get, and returns a non-None object (a str object). So the guard
clause can be retired; the ingloriously superobfuscatory original can
be compressed LEGIBLY into
   spaces = form.getvalue('spaces') == "1"





More information about the Python-list mailing list