[Python-3000] PEP: Drop Implicit String Concatentation

Raymond Hettinger python at rcn.com
Tue May 1 08:56:32 CEST 2007


PEP:  Remove Implicit String Concatenation

Motivation

One goal for Python 3000 should be to simplify the language by removing
unnecessary features.  Implicit string concatenation should be dropped in
favor of existing techniques. This will simplify the grammar and simplify a
user's mental picture of Python.  The latter is important for letting the
language "fit in your head".  A large group of current users do not even know
about implicit concatenation.  Of those who do know about it, a large portion
never use it or habitually avoid it. Of those both know about it and use it,
very few could state with confidence the implicit operator precedence and
under what circumstances it is computed when the definition is compiled versus
when it is run.

Uses and Substitutes

* Multi-line strings:

    s = "In the beginning, " \
        "there was a start."

    s = ("In the beginning," +
         "there was a start")

* Complex regular expressions are sometimes stated in terms of several
  implicitly concatenated strings with each regex component on a different
  line and followed by a comment.  The plus operator can be inserted here but
  it does make the regex harder to read.  One alternative is to use the
  re.VERBOSE option.  Another alternative is to build-up the regex with a
  series of += lines:

         r = ('a{20}'  # Twenty A's
              'b{5}'   # Followed by Five B's
              )

         r = '''a{20}  # Twenty A's
                b{5}   # Followed by Five B's
             '''                 # Compiled with thee re.VERBOSE flag

         r = 'a{20}'   # Twenty A's
         r += 'b{5}'   # Followed by Five B's

Automatic Substitution

When transitioning to Py3.0, some care should be taken to not blindly drop-in
a plus operator and possibly incur a change in semantics due to operator
precendence.  A pair such as:
         "abc" "def"
should be replaced using parentheses:
         ("abc" + "def")


More information about the Python-3000 mailing list