if the else short form

Hrvoje Niksic hniksic at xemacs.org
Wed Sep 29 13:38:48 CEST 2010


Tracubik <affdfsdfdsfsd at b.com> writes:

> 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.

The tutorial likely predates if/else expression syntax introduced in
2.5, which would be spelled as:

button = gtk.Button("True" if fill else "False")

BTW adding "==True" to a boolean value is redundant and can even break
for logically true values that don't compare equal to True (such as the
number 10 or the string "foo").



More information about the Python-list mailing list