[Python-ideas] Implicit string literal concatenation considered harmful?
Serhiy Storchaka
storchaka at gmail.com
Thu May 16 09:08:28 CEST 2013
10.05.13 21:48, Guido van Rossum написав(ла):
> I just spent a few minutes staring at a bug caused by a missing comma
> -- I got a mysterious argument count error because instead of foo('a',
> 'b') I had written foo('a' 'b').
>
> This is a fairly common mistake, and IIRC at Google we even had a lint
> rule against this (there was also a Python dialect used for some
> specific purpose where this was explicitly forbidden).
>
> Now, with modern compiler technology, we can (and in fact do) evaluate
> compile-time string literal concatenation with the '+' operator, so
> there's really no reason to support 'a' 'b' any more. (The reason was
> always rather flimsy; I copied it from C but the reason why it's
> needed there doesn't really apply to Python, as it is mostly useful
> inside macros.)
As was said before the '+' operator has less priority than the '%'
operator and an attribute access, i.e. it requires parenthesis in some
cases. However parenthesis introduce a noise and can cause other types
of errors.
In all cases only multiline implicit string literal concatenation cause
problem. What if forbid implicit string literal concatenation only
between string literals on different physical lines? A deliberate string
literal concatenation can be made with explicit line joining.
raise ValueError('Type names and field names must be valid '\
'identifiers: %r' % name)
raise ValueError('{} not bottom-level directory in '\
'{!r}'.format(_PYCACHE, path))
ignore_patterns = (
'Function "%s" not defined.' % breakpoint,
"warning: no loadable sections found in added symbol-file"\
" system-supplied DSO",
"warning: Unable to find libthread_db matching"\
" inferior's thread library, thread debugging will"\
" not be available.",
"warning: Cannot initialize thread debugging"\
" library: Debugger service failed",
'warning: Could not load shared library symbols for '\
'linux-vdso.so',
'warning: Could not load shared library symbols for '\
'linux-gate.so',
'Do you need "set solib-search-path" or '\
'"set sysroot"?',
)
I think this introduces less noise than the '+' operator or other
proposed alternatives.
More information about the Python-ideas
mailing list