[Python-ideas] Implicit string literal concatenation considered harmful?
Jonathan Eunice
jonathan.eunice at gmail.com
Sat May 11 00:58:11 CEST 2013
Implicit line concatenation is one of those rare places where Python
turns an oddly blind eye to the "Explicit is better than implicit"
rule it otherwise loves.
I can't speak to how much inconvenience/breakage it would cause,
but deprecating it would seem to increase the language's "coherence"
with--or at least, adherence to--its principles.
But I have no real stake in it; I seldom if ever use the construct.
I prefer a trick I learned in Perl: Using a "here document" cleanup
function. This allows multi-line literal strings to be stated in a
program, at whatever level of indentation is appropriate for code
clarity, and the indentation to be automatically removed. For
example, using [textdata](https://pypi.python.org/pypi/textdata):
from textdata import *
data = lines("""
There was an old woman who lived in a shoe.
She had so many children, she didn't know what to do;
""")
will result in:
['There was an old woman who lived in a shoe.',
"She had so many children, she didn't know what to do;"]
This is dedented, but also had some blank lines at the start
and end removed (the blanks make Python formatting look good,
but might gunk up subsequent processing). `lines` can also
do what implicit concatenation does:
data = lines(join=True, text="""
this
that
""")
gives a purely concatenated result:
'thisthat'
The `join` kwarg, given a string, will join on that string. Some
edge-case options aside, being able to state indented literal strings
without fussing with line-by-line quoting is the jewel.
Especially if Python's implicit line concatenation is going to be
deprecated, you might consider adding an "ease of multi-line string
specification" function like `lines` to the standard library (in
`textwrap`?) to ease the passing.
More information about the Python-ideas
mailing list