Python syntax wart

Carl Banks pavlovevidence at gmail.com
Mon Sep 10 18:30:03 EDT 2007


On Sep 10, 8:07 am, TheFlyingDutchman <zzbba... at aol.com> wrote:
> On Sep 9, 11:20 pm, TheFlyingDutchman <zzbba... at aol.com> wrote:
>
> > It may be that a language that doesn't have a statement terminator
> > (which can be end-of-line) needs a statement continuation symbol.
> > (Excluding languages like Lisp that have parentheses everywhere).
>
> Actually I guess Python does have a statement terminator - end-of-
> line.
> If you use end-of-line as your statement terminator it is difficult
> and maybe impossible
> to avoid a line-continuation character in those situations where you
> don't want
> an end-of-line to terminate your statement.

Well, a good chunk of line continuations can be done implicitly with
parentheses (one of the few places where implicit is better than
explicit).

Problem is, there are a few places where you might to break line that
parentheses aren't legal.  Back in the day, import statements were
like this, but the designers of Python artifically added the ability
to use parentheses there.  Nowadays, they're rarely needed.  One of
their main remaining uses is in opening multiline strings, like so:

mls = """\
This is line 1.
This is line 2.
I like all my lines lined up.
"""

The OP seems to be complaining that you can't arbitrarily break lines
in the middle of a statement, so that he can write ridiculously
formatted code.  It's not impossible to do what he asks; for example,
the following hypothetical code is not ambiguous.

for
     x
in
     range(10)
:
     print x

It isn't legal to end the statement anywhere before the colon, so it
would be possible to add a rule to Python's parser that says, "there
shall be implicit line continuations everywhere between the opening
keyword and the colon".

To be honest, I've always thought this would be a nice feature to
have, but now that I've seen what kind of abominations are possible
with it, I'm not so sure.

Carl Banks



P.S. The "two dimensional structure" can currently be done like this,
not that I believe such a travesty should ever be used in real code.

for(
    x
)in(
    range(10)
):
    print x




More information about the Python-list mailing list