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

M.-A. Lemburg mal at egenix.com
Fri May 10 21:46:44 CEST 2013


On 10.05.2013 21:30, Guido van Rossum wrote:
> On Fri, May 10, 2013 at 12:16 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> On Fri, 10 May 2013 11:48:51 -0700
>> Guido van Rossum <guido at python.org> wrote:
>>> 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.)
>>>
>>> Would it be reasonable to start deprecating this and eventually remove
>>> it from the language?
>>
>> I'm rather -1. It's quite convenient and I don't want to add some '+'
>> signs everywhere I use it. I'm sure many people also have long string
>> literals out there and will have to endure the pain of a dull task to
>> "fix" their code.
> 
> Fixing this is an easy task for lib2to3 though.

Think about code written to work in Python 2 and 3.

Python 2 would have to get the compile-time concatenation as well,
to prevent slow-downs due to run-time concatenation. And there would
have to be a tool to add the '+' signs and parens
to the Python 2 code...

s = ('my name is %s and '
     'I live on %s street' % ('foo', 'bar'))

-->

s = ('my name is %s and ' +
     'I live on %s street' % ('foo', 'bar'))

results in:
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: not all arguments converted during string formatting

The second line is also a good example of how removing the feature
would introduce a new difficult to see error :-)

IMO, the issue is a task for an editor or a lint tool to highlight,
not the Python compiler, IMO.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 10 2013)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2013-05-07: Released mxODBC Zope DA 2.1.2 ...     http://egenix.com/go46
2013-05-06: Released mxODBC 3.2.3 ...             http://egenix.com/go45

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-ideas mailing list