Question For Language Lawyers - WAS: Re: Combining colon statements on one line?

Delaney, Timothy C (Timothy) tdelaney at
Tue Jul 20 03:00:20 CEST 2004

Tim Daneliuk wrote:

> Phil Frost wrote:
>> newdate = date + (1000,2000)[ date < 500 ]
> OK, this is clever

I'd disagree that this is clever - I'd say that this is a PITA. In
particular, the most common error when using this "cute" trick is
getting the options in the wrong order. This stems from the fact that we
are used to the true condition first i.e.

    if true_condition:

and naturally this carries over to thinking it should be:

    a = [true_result, false_result][bool]

which is the exact opposite of what is needed. It's not helped any by
the fact that the C conditional is:

    bool ? true_result : false_result

Even with TDD, it's simple to end up with the values transposed. And if
you get it right, people still have to think "is this right" every time
they look at it, because it's the opposite of what they expect.

The sooner this "cute" trick dies out, the better IMO.

Now, addressing your question:

>     It is really good style to depend upon boolean True and False
>     being 1 and 0?  It seems to me that, especially in an OO
>     language, True and False ought to be treated as abstract
>     boolean objects.  In particular, assuming they are 1 and 0
>     (it seems to me) promotes something about the language
>     _implementation) into the application code which is a Bad
>     Thing.

True and False are explicitly documented as equalling 1 and 0
respectively. Furthermore, it is documented that bool is a subclass of
int. You can rely on these - they are not implementation details.

Tim Delaney

More information about the Python-list mailing list