Unexpected string behaviour: txt = 'this' ' works'
tenax.raccoon at gmail.com
Wed Feb 11 21:09:57 CET 2009
On Feb 11, 5:16 am, bearophileH... at lycos.com wrote:
> christopher.saun... at durham.ac.uk (c d saunter):
> >I assume it is a feature of the compiler.<
> Yes it's a bug-prone antifeature that's probably a relic from C that
> doesn't have a concatenation operator among strings as Python does
> (+). So in Python it saves you to use + at the cost of possible bugs.
I've used this feature in C and Python when I want to wrap strings
without having a newlines in the strings... grouping the whole bunch
with a set of parenthesis to make the concatenation explicit.
Admittedly, I wouldn't be broken up if this feature became deprecated,
It does do a minor optimization in Python and most C compilers. The
two string constants are concatenated when the code is parsed, rather
than when the code is executed.
>>> from dis import dis
>>> def f():
... return 'This is ' 'an example.'
2 0 LOAD_CONST 1 ('This is an example.')
It's such a minor optimization, that you probably wouldn't see any
effect on your program.
More information about the Python-list