[Python-ideas] Allowing comments after line continuations

M.-A. Lemburg mal at egenix.com
Fri May 17 09:30:50 CEST 2013


On 16.05.2013 20:41, Bruce Leban wrote:
> At Chris Angelico's suggestion, starting another thread on this:
> 
> The \ line continuation does not allow comments yet statements that span
> multiple lines may need internal comments. Also spaces after the \ are not
> allowed but trailing spaces are invisible to the reader but not to the
> parser. If you use parenthesis for continuation then you can add comments
> but there are cases where parenthesis don't work, for example, before in a
> with statement, as well as the current discussion of using \ to make
> implicit string concatenation explicit. So I propose adopting this rule for
> trailing \ continuation:
> 
> The \ continuation character may be followed by white space and a comment.
> If a comment is present, there must be at least one whitespace character
> between the \ and the comment.
> 
> 
> That is:
> 
> x = y + \   # comment allowed here
>     z
> 
> with a as x, \   # comment here may be useful
>      b as y, \   # or here
>      c as z: \   # or here
>     pass
> 
> x = y +     # syntax error
>     z
> 
> Two reasons for requiring a space after the backslash:
> 
> (1) make the backslash more likely to stand out visually (and we can't
> require a space before it)
> 
> (2) \# looks like it might be an escape sequence of some sort while I don't
> think \ # does, making this friendlier to readers.
> 
> I'm not passionate about that detail if the rest of the proposal flies.

I'm -1 on making the backslash more attractive to use :-)

In most use cases, you can create much more readable code
by using parenthesis, which easily allow spanning statements
and expressions across multiple lines.

Those read better, work better in editors (automatic indentation)
and are less error prone than the backslash.

If there are common use cases left which can currently not be
handled by parens, I'd be +1 on fixing those.

Your "with" example would be one such case, since the
following currently gives a SyntaxError:

with (a as x,  # comment here may be useful
      b as y,  # or here
      c as z): # or here
    pass

-- 
Marc-Andre Lemburg
eGenix.com

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