[Tutor] How to break long lines?

eryksun eryksun at gmail.com
Sat Feb 23 04:02:16 CET 2013


On Fri, Feb 22, 2013 at 6:46 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> At worst, the string concatenation + operator will apply at runtime,
> which for a short string like this is not a big deal. But in practice,
> I would expect Python's "keyhole optimizer" to see that it is only
> string literals being concatenated, and perform constant-folding at
> compile-time.
>
> (Note: constant-folding is not a promise of the language. Not all
> Python versions or implementations will do this.)

http://en.wikipedia.org/wiki/Peephole_optimization

A bit of trivia. The peephole optimizer (PyCode_Optimize) in CPython
3.3 was redesigned to use a stack, which allows folding even complex
expressions involving constants:


3.3:

    >>> dis.dis(lambda: ('a'*2 + 'b'*2) * 2)
      1           0 LOAD_CONST               7 ('aabbaabb')
                  3 RETURN_VALUE

3.2.3:

    >>> dis.dis(lambda: ('a'*2 + 'b'*2) * 2)
      1           0 LOAD_CONST               4 ('aa')
                  3 LOAD_CONST               5 ('bb')
                  6 BINARY_ADD
                  7 LOAD_CONST               2 (2)
                 10 BINARY_MULTIPLY
                 11 RETURN_VALUE


More information about the Tutor mailing list