assigning multi-line strings to variables
lie.1296 at gmail.com
Fri Apr 30 12:37:56 CEST 2010
On 04/30/10 13:21, Steven D'Aprano wrote:
> On Fri, 30 Apr 2010 05:41:26 +1000, Lie Ryan wrote:
>> On 04/29/10 20:40, Gregory Ewing wrote:
>>> Lie Ryan wrote:
>>>> No, the implicit concatenation is there because Python didn't always
>>>> have triple quoted string. Nowadays it's an artifact and triple quoted
>>>> string is much preferred.
>>> I don't agree. I often use implicit concatenation when I'm writing a
>>> format string that won't fit on one source line, because it allows me
>>> to fit it into the surrounding indentation structure without
>>> introducing unwanted spaces into the string.
>>> Both tecnhiques have their places.
>> That statement should be quantified with "for large chunks of text".
>> Format string is typically 2-3 lines at most, not enough to qualify as
>> large chunk.
> No it shouldn't. It applies equally for two lines or two hundred lines.
> You seem to have missed that these are NOT equivalent:
> To avoid the ugly backslash, you can use Python's implicit line
> continuation inside brackets. This is especially handy since you often
> use brackets for function calls:
You *again* missed the point. The purpose here is to *avoid manual
preprocessing* of the text chunk string concatenation is not suitable
for including large text. Implicit because they force you to preprocess
the chunk before python will accept the text into the source code. If
you added backslashes inside triple-quotes, it's just as ugly as
When you don't want the newline, then just let the text flow, there is
no need to *artificially* break the lines, whether using backslash or
implicit concatenation. Triple quoted string is a place where 80-char
limits (or whatever number you set) shouldn't apply; if you insist on
having string inside triple-quotes to be obey 80-chars, then it's just
With triple quoting, you just need to copy, paste, then add *exactly
two* backslashes if preserving exact newline is crucial. Compare that
with adding a quote before and after *each line*. That is O(1) vs O(n)
Large chunk of text inside source code is usually there because you're
too lazy to create a separate file for it. When you're too lazy to even
create a file, you're not likely to be that industrious to do any sort
of preprocessing to the chunk. And you most likely won't care that the
source code looks ugly if it exceeds the oh-so-sacred 80-char limit,
because scripts with that kind of nature (i.e. includes large text chunk
instead of separate file) is typically one-off throwaway scripts. You
want to start working on the chunk immediately instead of spending
ten-minutes forcing python to grok the text.
More information about the Python-list