On Fri, Feb 22, 2013 at 6:46 PM, Steven D'Aprano <st...@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 _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor