"?:", "a and b or c" or "iif"

Tim Peters tim_one at email.msn.com
Wed May 26 23:08:43 EDT 1999


[Tim]
>>>> (x and [a] or [b])[0]-ly y'rs - tim

[Hrvoje Niksic]
>>> Can Python's optimizer optimize away the consing?

[Jeremy Hylton]
>> Which optimizer would that be?

[Hrvoje]
> Uh, the one that creates `.pyo' files as opposed to `.pyc'?

No, -O doesn't do much:  binds the builtin name __debug__ to 0 and skips
generating code for blocks of the form "if __debug__: xxx"; as a conceptual
consequence of that, skips generating code for "assert" stmts; and skips
generating opcodes to update the current file-relative line number (more of
a win than it may sound, since SET_LINENO is the most frequently executed
opcode yet doesn't get your program closer to its end).  That's it.  Does no
program analysis of any kind.

-OO goes on to throw away docstrings.

If the revolting "(x and [a] ..." got popular, a peephole optimizer could
get rid of the list ops -- but only at the cost of frustrating Michael and
Neel's attempts to change what the generated code maps "0" to <0.9 wink>.

(x and (a,) or (b,))[0]-is-faster-ly y'rs  - tim






More information about the Python-list mailing list