[Python-ideas] Implicit string literal concatenation considered harmful?

rurpy at yahoo.com rurpy at yahoo.com
Fri May 17 19:14:15 CEST 2013


On 05/17/2013 02:49 AM, Steven D'Aprano wrote:
> On 17/05/13 04:28, Ron Adam wrote:
> 
>> I think the line continuation '\' character would also make a good
>> explicit string literal concatenation character.  It's already
>> limited to only work across sequential lines as well.
> 
> Concatenating strings on the same line is a legitimate thing to do,
> because you can mix different quoting types. In my opinion, there's
> no really clean way to build string literals containing multiple
> types of quotation marks, but implicit concatenation works and is
> useful. Here's a contrived example:
> 
> 
> s = "'Aren't you supposed to be " '"working"?' "', he asked with a
> wink."

And here's a non-contrived one (almost) verbatim from 
working code:

 pattern = '[^\uFF1B\u30FB\u3001' r'+:=.,\/\[\]\t\r\n]+' '[\#\uFF03]+'

In Python 2 this had been:

 pattern = ur'[^\uFF1B\u30FB\u3001+:=.,\/\[\]\t\r\n]+[\#\uFF03]+'

but was changed to first form above due to Python 3's 
removal of lexical evaluation of \u literals in raw 
strings (see http://bugs.python.org/issue14973).

Obviously the concatenation could have been done with
the + operator but I felt the given form was clearer
than trying to visually get whether any particular "+" 
was inside or outside of a string.  There are other 
more complex regex with more +'s and my preference is 
to adopt a particular form I can use for most/all such 
rather than to tweak forms based on a particular 
string's content.

I am assuming this discussion is regarding a possible 
Python 4 feature -- adjacent string literal concatenation 
has been documented behavior of Python going back to at
least version 1.4.0 (the earliest doc available on 
python.org):

  "2.4.1.1 String literal concatenation
  Multiple adjacent string literals (delimited by
  whitespace), possibly using different quoting
  conventions, are allowed, and their meaning is
  the same as their concatenation..."

I also have been using adjacent string literal 
concatenation in the "help" parameters of argparse 
calls as standard practice for many years, albeit 
on separate lines.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130517/c7db26dc/attachment.html>


More information about the Python-ideas mailing list