Python's "only one way to do it" philosophy isn't good?

Josiah Carlson josiah.carlson at sbcglobal.net
Sun Jun 10 16:27:27 CEST 2007


Steven D'Aprano wrote:
> On Sat, 09 Jun 2007 22:52:32 +0000, Josiah Carlson wrote:
> 
>> the only thing that optimization 
>> currently does in Python at present is to discard docstrings
> 
> Python, or at least CPython, does more optimizations than that. Aside from
> run-time optimizations like interned strings etc., there are a small
> number of compiler-time optimizations done.
> 
> Running Python with the -O (optimize) flag tells Python to ignore
> assert statements. Using -OO additionally removes docstrings.

Oh yeah, asserts.  I never run with -O, and typically don't use asserts, 
so having or not having either isn't a big deal for me.

> Regardless of the flag, in function (and class?) definitions like the
> following:
> 
> def function(args):
>     "Doc string"
>     x = 1 
>     s = "this is a string constant"
>     "and this string is treated as a comment"
>     return s*x
> 
> The string-comment is ignored by the compiler just like "real" comments.
> (The same doesn't necessarily hold for other data types.)

I would guess it is because some other data types may have side-effects. 
  On the other hand, a peephole optimizer could be written to trim out 
unnecessary LOAD_CONST/POP_TOP pairs.

> Some dead code is also optimized away:

Obviously dead code removal happens regardless of optimization level in 
current Pythons.

> Lastly, in recent versions (starting with 2.5 I believe) Python includes a
> peephole optimizer that implements simple constant folding: 

Constant folding happens regardless of optimization level in current 
Pythons.


So really, assert and docstring removals.  Eh.

  - Josiah



More information about the Python-list mailing list