For review: PEP 308 - If-then-else expression
Roy Smith
roy at panix.com
Mon Feb 10 07:51:22 EST 2003
In article <b27d3p$gm$1 at slb9.atl.mindspring.net>,
"Andrew Dalke" <adalke at mindspring.com> wrote:
> That's why I analyzed code from a language which already has
> an if/else ternary expression. Eg, consider this example from Python's
> C implementation, in cellobject.c (first one I found, but I imagine
> there are plenty such examples)
>
> if (b->ob_ref == NULL)
> return 0;
> return -1;
>
> This could be written in C as
>
> return b->ob_ref == NULL ? 1 : 0
Except that you managed to both reverse the sense of the comparison and
the sign of the value returned if the comparison is false! Not that I
noticed that right away; it took me a few moments to work through
exactly what you wrote did. I'm not trying to pick on you, just point
out that the longer form, written over three lines, is easier to
understand.
It could have been written:
return - (b->ob_ref != NULL);
but I'd slap anybody who tried to bring something like that to a code
review :-)
More information about the Python-list
mailing list