[Tutor] syntax question
Kent Johnson
kent37 at tds.net
Wed May 21 18:10:50 CEST 2008
On Wed, May 21, 2008 at 8:27 AM, Eric Abrahamsen <girzel at gmail.com> wrote:
> Hi all,
>
> This is a syntax that I've seen on occasion in other people's code:
>
> theme = (VALID_THEMES[0], theme) [ theme in VALID_THEMES ]
>
> I have no idea how this works, or how to go about looking it up.
This is one way to implement a conditional expression in Python < 2.5.
In general, the expression
(A, B)[bool(C)]
has the value A if bool(C) is false and B if bool(C) is true.
The boolean value is either 0 or 1. It is used as an index into the
tuple to select the desired value.
In Python 2.5 this can be written with the condition expression
B if C else A
or to use your example
theme = theme if (theme in VALID_THEMES) else VALID_THEMES[0]
Kent
More information about the Tutor
mailing list