spaces = True if form.getvalue('spaces') == 1 if
form.has_key('spaces') else False else False
--
Anjanesh Lekshmnarayanan
If you've got any sense at all, you don't. Write the logic so it makes
some sense to the casual reader. You will be that casual reader in a few
months.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
That seems to just be an overly complicated way of writing:
spaces = bool(form.has_key('spaces') and form.getvalue('spaces') == 1)
Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
> How do I achieve something like this using python ?
> spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True : False : False)
if form.has_key('spaces'):
spaces = form.getvalue('spaces') == 1
else:
spaces = False
However, my code is not acheiving entirely the same thing as
your example code. The object 'spaces' ends up with the same
value, but my code fails to acheive the other presumed
objective: confusing the reader.
--
Grant
spaces = form.has_key('spaces') and form.getvalue('spaces') == 1
or even (this is about the cgi module, I think)
spaces = form.getvalue('spaces', None) == 1
But the == 1 comparison will always fail, since getvalue returns a string.
> But the == 1 comparison will always fail, since getvalue returns a string.
How do we know that from the what the OP posted?
--
Grant
But I just dont see how
spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ?
True: False : False)
is complicated in anyway. Its not that hard to read at all.
Thanks.
You have /nested/ /ternary/ expressions /without/ any disambiguating
parentheses. And you unnecessarily use boolean constants. The latter
and the stuff in italics is what makes your code hard to understand.
As I showed, you can write the *exact same thing* without using
ternary expressions at all (win!), much less nested ones, and without
any boolean constants.
And it's even 16 characters shorter.
Better:
spaces = bool(('spaces' in form) and form.getvalue('spaces') == 1)
Bye,
bearophile
+1 QOTW !-)
Bruhahaa, my biggest laugh since months
--
Christian Tismer :^) <mailto:tis...@stackless.com>
tismerysoft GmbH : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56 mobile +49 173 24 18 776 fax +49 30 80 90 57 05
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
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"
Is it still necessary to convert the result (and of two comparison
operations) to bool?
Mel.