[Python-Dev] String literal concatenation & docstrings

Brett C. bac at OCF.Berkeley.EDU
Fri Nov 26 20:56:05 CET 2004


Nick Coghlan wrote:
> Michael Hudson wrote:
> 
>> Nick Coghlan <ncoghlan at iinet.net.au> writes:
>> I haven't actually checked or anything rash like that, but I'd imagine
>> the answer is something like:
>>
>>    The two strings are separate statements as far as the parser is
>>    concerned, and the "concatenating adjacent strings" thing only
>>    happens within an expression.
> 
> 
> That would certainly be a sensible explanation. The only time I've ever 
> actually made use of the feature is when assigning a long string, and 
> even then only rarely (I'm more likely to use triple quotes and left 
> align the whole thing)
> 

And the sensible explanation is correct.  Just checked out the compiler and the 
string concatenation (in parsestrplus()) takes a node and then proceeds to 
concatenate all of its children that are strings right in a row starting at 
child 0.  With statements this won't trigger anything since the statements will 
only have the string as their child, unlike an expression, which will just have 
all the string pieces.

[SNIP]
> I think the key distinction I'd missed was that in the doc string case, 
> the two strings were actually separate statements. Once that distinction 
> is noted, the behaviour is, as you say, unsurprising. It also makes it 
> obvious why escaping the newline has the effect it does.
> 

Should probably change the wording on that unless people actually want the 
literal string concatenation to work with statements (docstrings seem like the 
only place that would be reasonable) unless you want to start allowing print 
statements to have a string part span multiple lines.  =)

-Brett


More information about the Python-Dev mailing list