Python syntax wart

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 9 04:58:21 EDT 2007


On Sun, 09 Sep 2007 17:16:05 +1200, Lawrence D'Oliveiro wrote:

> The one thing I don't like about Python syntax is using backslashes to
> continue lines.

Then don't use them. Put everything in one long line.

Or do something like this. Instead of

for Link in GetEachRecord("lots", "and", "lots", "of", "arguments"):

you can do this:

args = ("lots", "and", "lots", "of", "arguments")
for Link in GetEachRecord(*args):



> 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


That is quite possibly the ugliest piece of code I've ever seen in 
Python. I'm impressed. Did you format it yourself or did you use a 
professionally written code-uglifier?

Or perhaps I should say:

That
    is
    quite possibly
        the
        ugliest piece
        of code
            I
            '
            ve
        ever seen
    in
        Python
    .
    # end sentence

But have you actually tried it before declaring it isn't possible? After 
writing a couple of short stub functions, then copy-and-pasting your 
code, it worked perfectly for me.

For the record, here are the stubs I used:

class Stub(object):
    def write(self, *args):
        print "Stub called"

def GetEachRecord(*args):
    return [{"from_episode": i} for i in range(5)]

def LinkToMe(*args):
    return "stub"

out = Stub()
EpisodeID = 1234

Now copy the "Not possible" code from Lawrence's original post. Make sure 
you adjust the indentation of the first line, i.e. there should be no 
white space before the "for". Paste the whole lot into your Python 
interpreter, and you should get "Stub called" printed five times.



-- 
Steven.



More information about the Python-list mailing list