if the else short form

Seebs usenet-nospam at seebs.net
Wed Sep 29 13:20:39 EDT 2010


On 2010-09-29, Tracubik <affdfsdfdsfsd at b.com> wrote:
> Hi all,
> I'm studying PyGTK tutorial and i've found this strange form:
>
> button = gtk.Button(("False,", "True,")[fill==True])
>
> the label of button is True if fill==True, is False otherwise.
>
> i have googled for this form but i haven't found nothing, so can any of 
> you pass me any reference/link to this particular if/then/else form?

Oh, what a nasty idiom.

Here's the gimmick.
	("False,", "True,")
is a tuple.  That means you can index it.  For instance:
	("False,", "True,")[0]
is the string "False,".

So, what is the numeric value of "fill == True"?  Apparently, at least
at the time this was written, it was 0 if fill was not equal to True,
and 1 if fill was equal to True.

Let me say, though, that I'm a C programmer, so I'm coming from a language
where the result of 0-or-1 for test operators is guaranteed, and I still
wouldn't use this in live code.  It's insufficiently legible.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nospam at seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.



More information about the Python-list mailing list