<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
On 29/09/10 9:20 PM, <a class="moz-txt-link-abbreviated" href="mailto:python-list-request@python.org">python-list-request@python.org</a> wrote:
<blockquote
cite="mid:mailman.5809.1285759202.29447.python-list@python.org"
type="cite">
<table class="header-part1" width="100%" border="0"
cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<div class="headerdisplayname" style="display: inline;">Subject:
</div>
if the else short form</td>
</tr>
<tr>
<td>
<div class="headerdisplayname" style="display: inline;">From:
</div>
Tracubik <a class="moz-txt-link-rfc2396E" href="mailto:affdfsdfdsfsd@b.com"><affdfsdfdsfsd@b.com></a></td>
</tr>
<tr>
<td>
<div class="headerdisplayname" style="display: inline;">Date:
</div>
29 Sep 2010 10:42:37 GMT</td>
</tr>
</tbody>
</table>
<table class="header-part2" width="100%" border="0"
cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<div class="headerdisplayname" style="display: inline;">To:
</div>
<a class="moz-txt-link-abbreviated" href="mailto:python-list@python.org">python-list@python.org</a></td>
</tr>
</tbody>
</table>
<br>
<div class="moz-text-plain" wrap="true" graphical-quote="true"
style="font-family: -moz-fixed; font-size: 12px;"
lang="x-unicode">
<pre wrap="">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?
</pre>
</div>
</blockquote>
<br>
As others have pointed out, a tuple with two items is created and
then indexed by the integer conversion of conditional. It is
"creative" coding but I wouldn't recommend using it either.<br>
<br>
For this particular case you may achieve the same thing with:<br>
<pre> button = gtk.Button(str(fill==True))
</pre>
or possibly just:<br>
<pre> button = gtk.Button(fill==True)
</pre>
It's a little better but still not great.<br>
<br>
A verbose form of something more generic may be:<br>
<blockquote>
<pre>if fill:</pre>
<pre> label = 'True'</pre>
<pre>else:</pre>
<pre> label = 'False'</pre>
<pre>button = gtk.Button(label)</pre>
</blockquote>
or possibly:<br>
<blockquote>
<pre>label = 'True' if fill else 'False'
button = gtk.Button(label)</pre>
</blockquote>
or using a dict for label lookup:<br>
<blockquote>
<pre>label = { True : 'True', False : 'False' }
button = gtk.Button(label[fill])</pre>
</blockquote>
<br>
Cheers, Brendan.<br>
<br>
</body>
</html>