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. 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.) Some dead code is also optimized away: >>> def function(): ... if 0: ... print "dead code" ... return 2 ... >>> dis.dis(function) 4 0 LOAD_CONST 1 (2) 3 RETURN_VALUE Lastly, in recent versions (starting with 2.5 I believe) Python includes a peephole optimizer that implements simple constant folding: # Python 2.4.3 >>> dis.dis(lambda: 1+2) 1 0 LOAD_CONST 1 (1) 3 LOAD_CONST 2 (2) 6 BINARY_ADD 7 RETURN_VALUE # Python 2.5 >>> dis.dis(lambda: 1+2) 1 0 LOAD_CONST 2 (3) 3 RETURN_VALUE The above all holds for CPython. Other Pythons may implement other optimizations. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list