PEP8 compliance and exception messages ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Dec 6 01:52:53 EST 2010


On Mon, 06 Dec 2010 06:15:06 +0000, Tim Harig wrote:

>> But isn't explicit string literal concatenation better than implicit
>> string literal concatenation?
> 
> So add the "+", it really doesn't change it much.

Perhaps not *much*, but it *may* change it a bit.

Implicit concatenation of literals is promised to be handled by the 
compiler, at compile time:

>>> from dis import dis
>>> dis(compile("s = 'hello' 'world'", "", "single"))
  1           0 LOAD_CONST               0 ('helloworld')
              3 STORE_NAME               0 (s)
              6 LOAD_CONST               1 (None)
              9 RETURN_VALUE

This holds all the way back to Python 1.5 and probably older.

But explicit concatenation may occur at run-time, depending on the 
implementation and the presence or absence of a keyhole optimizer. E.g. 
in Python 2.4:

>>> dis(compile("s = 'hello' + 'world'", "", "single"))
  1           0 LOAD_CONST               0 ('hello')
              3 LOAD_CONST               1 ('world')
              6 BINARY_ADD
              7 STORE_NAME               0 (s)
             10 LOAD_CONST               2 (None)
             13 RETURN_VALUE



A small difference, but a real one.


-- 
Steven



More information about the Python-list mailing list