For review: PEP 308 - If-then-else expression

James J. Besemer jb at
Mon Feb 10 20:53:45 CET 2003

Andrew Dalke wrote:

> This could be written in C as
>    return  b->ob_ref == NULL ? 1 : 0
> but wasn't.  Why wasn't it?
> To repeat, just because the language supports a ?:-like expression
> doesn't mean it's used.  I believe it is used many fewer times
> than it can be used.

This is just rhetoric.  It doesn't prove anything.

Nobody is saying ?: or equivalent is the best way to express your self in 
every single instance.

The arguments FOR conditional expressions generally involve side effects, or 
the readability/maintainability improvement of factoring out the common 
assignment from the individual terms.  Since there are no side effects in 
this and presumably many of your 'hits', then the two forms are genuinely 
equivalent and you would expect to see a mix.

Too, your conclusion assumes that the code was all written by a single 
person.  In all likelihood, the code was written by different people, with 
different knowledge levels and individual prejudices.  It seems foolish to 
expect ?: to appear everywhere in a C program where it might fit, about as 
realistic to expect it would never appear.

To answer your question "why wasn't it" generally:

1. maybe the person who wrote that particular piece of code
didn't know about  ?:

2. maybe the person knew ?: but didn't think to use it.

3. maybe the person in question, like you, knew all about ?:
but was ideologically opposed to using it.

4. maybe the person for some reason thought the given expression
was more clear in this case even though he knows about and uses :?
routinely in other code.

5. The code in question is the result of a bug, with the
"return -1" accidentally having been omitted.  Simply adding
the additional statement was the easiest mod for the maint.
engineer to make and he didn't think to rewrite the surrounding

6. I am sure there are other possibilities.

The fact that some sample code which could have used the construct did not 
use it doesn't denigrate the usefulness of the construct.

More importantly, your doesn't constitute an argument that the construct is bad.



James J. Besemer		503-280-0838 voice
2727 NE Skidmore St.		503-280-0375 fax
Portland, Oregon 97211-6557	mailto:jb at	

More information about the Python-list mailing list