Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Multiple conditional expression

4 views
Skip to first unread message

Anjanesh Lekshminarayanan

unread,
Feb 26, 2009, 10:11:18 PM2/26/09
to Python List
How do I achieve something like this using python ?
spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True
: False : False)

spaces = True if form.getvalue('spaces') == 1 if
form.has_key('spaces') else False else False
--
Anjanesh Lekshmnarayanan

Steve Holden

unread,
Feb 26, 2009, 10:14:50 PM2/26/09
to pytho...@python.org
Anjanesh Lekshminarayanan wrote:
> How do I achieve something like this using python ?
> spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True
> : False : False)
>
> spaces = True if form.getvalue('spaces') == 1 if
> form.has_key('spaces') else False else False

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/

Chris Rebert

unread,
Feb 26, 2009, 10:17:20 PM2/26/09
to Anjanesh Lekshminarayanan, Python List
On Thu, Feb 26, 2009 at 7:11 PM, Anjanesh Lekshminarayanan
<ma...@anjanesh.net> wrote:
> How do I achieve something like this using python ?
> spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True
> : False : False)
>
> spaces = True if form.getvalue('spaces') == 1 if
> form.has_key('spaces') else False else False

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

Grant Edwards

unread,
Feb 26, 2009, 10:26:09 PM2/26/09
to
On 2009-02-27, Anjanesh Lekshminarayanan <ma...@anjanesh.net> wrote:

> 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

Paul Rubin

unread,
Feb 26, 2009, 10:34:29 PM2/26/09
to
Chris Rebert <cl...@rebertia.com> writes:
> spaces = bool(form.has_key('spaces') and form.getvalue('spaces') == 1)

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.

Grant Edwards

unread,
Feb 26, 2009, 11:33:24 PM2/26/09
to
On 2009-02-27, Paul Rubin <http> wrote:

> But the == 1 comparison will always fail, since getvalue returns a string.

How do we know that from the what the OP posted?

--
Grant

Anjanesh Lekshminarayanan

unread,
Feb 26, 2009, 11:39:39 PM2/26/09
to Python List
> How do we know that from the what the OP posted?
Its CGI alright.
spaces = form.has_key('spaces') and form.getvalue('spaces') == '1'

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.

Chris Rebert

unread,
Feb 27, 2009, 12:11:28 AM2/27/09
to Anjanesh Lekshminarayanan, Python List
On Thu, Feb 26, 2009 at 8:39 PM, Anjanesh Lekshminarayanan
<ma...@anjanesh.net> wrote:
>> How do we know that from the what the OP posted?
> Its CGI alright.
> spaces = form.has_key('spaces') and form.getvalue('spaces') == '1'
>
> 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.

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.

bearoph...@lycos.com

unread,
Feb 27, 2009, 3:55:06 AM2/27/09
to
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)

Bye,
bearophile

Steve Holden

unread,
Feb 27, 2009, 8:13:35 AM2/27/09
to pytho...@python.org
Anjanesh Lekshminarayanan wrote:
>> How do we know that from the what the OP posted?
> Its CGI alright.
> spaces = form.has_key('spaces') and form.getvalue('spaces') == '1'
>
> 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.
>
In that case I fear Python will be far too simple for your clearly
impressive mind. You've been programming for way too long ...

Bruno Desthuilliers

unread,
Mar 2, 2009, 3:53:40 AM3/2/09
to
Steve Holden a écrit :

> Anjanesh Lekshminarayanan wrote:
>>> How do we know that from the what the OP posted?
>> Its CGI alright.
>> spaces = form.has_key('spaces') and form.getvalue('spaces') == '1'
>>
>> 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.
>>
> In that case I fear Python will be far too simple for your clearly
> impressive mind. You've been programming for way too long ...

+1 QOTW !-)

Christian Tismer

unread,
Mar 2, 2009, 4:03:35 AM3/2/09
to Bruno Desthuilliers, pytho...@python.org

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/

John Machin

unread,
Mar 2, 2009, 10:19:21 AM3/2/09
to

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"


Mel

unread,
Mar 3, 2009, 6:53:12 PM3/3/09
to

Is it still necessary to convert the result (and of two comparison
operations) to bool?

Mel.

John Machin

unread,
Mar 3, 2009, 7:07:46 PM3/3/09
to
On Mar 4, 10:53 am, Mel <mwil...@the-wire.com> wrote:


Still? It never was necessary.

0 new messages