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

Bruce Leban bruce at leapyear.org
Sat May 11 02:29:28 CEST 2013


I got bit by this quite recently, leaving out a comma in a long list of
strings and I only found the bug by accident.

This being python "ideas" I'll throw one out.

Add another prefix character to strings:

    a = [m'abc'
         'def']   # equivalent to ['abcdef']

A string with an m prefix is continued on one or more following lines. A
string must have an m prefix to be continued (but this change would have to
be phased in). A conversion tool need merely recognize the string
continuations and insert m's. I chose the m character for multi-line but
the character choice is available for bikeshedding. The m prefix can be
combined with u and/or r but not with triple-quotes. The following are not
allowed:

    b = ['abc'    # syntax error (m is required for continuation)
         'def')

    c = [m'abc']  # syntax error (when m is used, continuation lines must
be present)

    d = [m'abc'
         m'def']  # syntax error (m only allowed for first string)

The reason to prohibit cases c and d guard against comma errors with these
forms. Consider these cases with missing or extra commas.

    e = [m'abc',  # extra comma causes syntax error
         'def']

    f = [m'abc'   # missing comma causes syntax error
         m'def',
         'ghi']

Yes, I know this doesn't guard against all comma errors. You could protect
against more with prefix and suffix (e.g., an m at the end of the last
string) but I'm skeptical it's worth it.

 Conversion to this could be done in three stages:

(1) accept m's (case a), deprecate missing m's (case b), error for misused
m's (case c-f)
(2) warn on missing m's (case b)
(3) error on missing m's (case b)


--- Bruce
Latest blog post: Alice's Puzzle Page http://www.vroospeak.com
Learn how hackers think: http://j.mp/gruyere-security
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130510/32b60c7b/attachment.html>


More information about the Python-ideas mailing list