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

M.-A. Lemburg mal at egenix.com
Sat May 11 23:14:14 CEST 2013


On 11.05.2013 20:37, Christian Tismer wrote:
> On 11.05.13 19:24, M.-A. Lemburg wrote:
>> On 11.05.2013 19:05, Christian Tismer wrote:
>>> I think a simple stripping of white-space in
>>>
>>>      text = s"""
>>>        leftmost column
>>>          two-char indent
>>>        """
>>>
>>> would solve 95 % of common indentation and concatenation cases.
>>> I don't think provision for merging is needed very often.
>>> If text occurs deeply nested in code, then it is also quite likely to
>>> be part of an expression, anyway.
>>> My major use-case is text constants in a class or function that
>>> is multiple lines long and should be statically ready to use without
>>> calling a function.
>>>
>>> (here an 's' as a strip prefix, but I'm not sold on that)
>> This is not a good solution for long lines where you don't want to
>> have embedded line endings. Taken from existing code:
>>
>> _litmonth = ('(?P<litmonth>'
>>               'jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|'
>>               'mär|mae|mrz|mai|okt|dez|'
>>               'fev|avr|juin|juil|aou|aoû|déc|'
>>               'ene|abr|ago|dic|'
>>               'out'
>>               ')[a-z,\.;]*')
>>
>> or
>>                      raise errors.DataError(
>>                          'Inconsistent revenue item currency: '
>>                          'transaction=%r; transaction_position=%r' %
>>                          (transaction, transaction_position))
>>
>> We usually try to keep the code line length under 80 chars,
>> so splitting literals in that way is rather common, esp. in
>> nested code paths.
>>
> 
> Your first example is a regex, which could be used as-is.
> 
> Your second example is indented five levels deep. That is a coding
> style which I would propose to write differently for better readability.
> And if you stick with it, why not use the "+"?
> 
> I want to support constant strings, which should not be somewhere
> in the middle of code. Your second example is computed, anyway,
> not the case that I want to solve.

You're not addressing the main point I was trying to make :-)

Triple-quoted strings work for strings that are supposed to
have embedded newlines, but they don't provide a good alternative
for long strings without embedded newlines.

Regarding using '+' in these cases: of course that would be
possible, but it clutters up the code, often requires additional
parens, it's slower and can lead to other weird errors when
forgetting parens, which are not much different than the one
Guido mentioned in his original email.

In all the years I've been writing Python, I've only very rarely
had an issue with missing commas between strings. Most cases I
ran into were missing commas in lists of tuples, not strings:

        l = [
            'detect_target_type',
            (None, Is, '"', +1, 'double_quoted_target')
            (None, Is, '\'', +1, 'single_quoted_target'),
            (None, IsIn, separators, 'unquoted_target', 'empty_target'),
            ]

This gives:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
TypeError: 'tuple' object is not callable

:-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, May 11 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