PEP 308 - ternary operator

Wolfgang Strobl wks000 at t-online.de
Sun Feb 16 08:18:09 EST 2003


Jim Jinkins <j-jinkins at usa.net>:

>My preference for an immediate-if operator is the traditional '?' from C:
>
>    cond ? expr1 : expr2
>

I'm not quite shure whether this is old news or not, but I haven't seen
the following mentioned here. 

Jason Orendorffs entry, a tiny version of "Collossal Cave", 

http://www0.us.ioccc.org/2001/jason.c resp http://www.ioccc.org/

got a "best of show" award in the latest International Obfuscated C
Code Contest.  IMHO, a great part of its obfuscation is owed to Cs
ternary operator. For example, the state machine that runs the game is
a single loop evaluating a single expression composed of a rats nest of
thirty six  ?: and thirty six logical ors - without a single bracket! 


>Yes this form is familiar only to C programmers, but there are a lot of 
>us.  Yes it is one more use of the ':' operator, but Guido admits it is 
>not ambiguous.  It also allows compact expressions of the form
>
>    cond ? expr1 : cond ? expr2 : cond ? expr2 : ...

Right. It even allows nightmares like

  h (*p++ - ' ' || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? 7 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? 36 : 59 * 89 * 5 * 2 : *p++ - ' '
     || !p++ ? 6587 * 77 : 9 + 2 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? 9 : 5 : 37 : *p++ - ' '
     || !p++ ? 5 + 5 : 4 : 20 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? 32 : 431 * 4 * 59 * 17 : *p++ - ' '
     || !p++ ? 17 : 34 : 19 : *p++ - ' ' || !p++ ? 8 * 2 : *p++ - ' '
     || !p++ ? 2 : 26 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? 82 * +576 : *p++ - ' '
     || !p++ ? 33 : 22 : 5 * 5 : 3 * 4 : 3 : *p++ - ' '
     || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? 6 + +17 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? 13 : 6 : 9 * 2 : *p++ - ' '
     || !p++ ? 15 : 7 * 2 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? 27 : 24 : 31 : *p++ - ' ' || !p++ ? *p++ - ' '
     || !p++ ? +29 : 30 : 1);

I had to look at the c parse tree in order to understand what it does. 


-- 
Thank you for observing all safety precautions




More information about the Python-list mailing list