Python syntax wart

Jason tenax.raccoon at gmail.com
Mon Sep 10 09:47:46 EDT 2007


On Sep 8, 11:16 pm, Lawrence D'Oliveiro <l... at geek-
central.gen.new_zealand> wrote:
> The one thing I don't like about Python syntax is using backslashes to
> continue lines. Yes, you can avoid them if you can include parentheses
> somehow, but this isn't always possible.
>
> Possible:
>
>     if (
>             quitting
>         and
>             len(client["to_write"]) == 0
>         and
>             len(client["read"]) + client["to_read"] == 0
>     ) :
>         close_client(client, "shutting down")
>     #end if
>
> Not possible:
>
>     for \
>         Link \
>     in \
>         GetEachRecord \
>           (
>             "links",
>             ("from_episode",),
>             "to_episode = %s",
>             [EpisodeID],
>             "order by when_created"
>           ) \
>     :
>         out.write \
>           (
>                 "<P><A HREF=\"%s\">Back to episode %d</A>\n"
>             %
>                 (
>                     LinkToMe({"ep" : Link["from_episode"]}),
>                     Link["from_episode"]
>                 )
>           )
>     #end for

Python doesn't prevent you from writing ugly code, using poor variable
names, or doing other silly things.  The rules on indention require
that you are at least consistent on a given line.  That said, the
backslash simply continues the line, and I don't see why your (ugly,
IMO) code is impossible.  Here's some similarly schizophrenically
formatted code that I just tried:

>>> for \
...     l\
... in \
...     (\
...             1,
...                 2,
...             3
...     )\
... :
...     print\
...             "This is "\
...                     "poorly "\
...                                             "formatted!",\
... l
...
This is poorly formatted! 1
This is poorly formatted! 2
This is poorly formatted! 3
>>>

Looks like it runs to me.  In general, I'd recommend that you avoid
such nonsense spacing and alignment, and use wrappers, generators, and
other Python constructs to help write more sensible code.

  --Jason




More information about the Python-list mailing list