Python syntax wart

  • Thread starter Lawrence D'Oliveiro
  • Start date
L

Lawrence D'Oliveiro

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
 
M

Marc 'BlackJack' Rintsch

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:

[…]

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

What do you mean by not possible!? This compiles fine for me.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Stefan Behnel

Marc said:
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:

[…]

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

What do you mean by not possible!? This compiles fine for me.

He means he has to use backslashes instead of parentheses here.

Which is not true, you could easily rephrase this as:

for link in GetEachRecord(
"links",
....):
out.write(
....
)

See? No backslash!

Stefan
 
L

Lawrence D'Oliveiro

He means he has to use backslashes instead of parentheses here.

Which is not true, you could easily rephrase this as:

for link in GetEachRecord(
"links",
....):
out.write(
....
)

See? No backslash!

But then you can no longer use indentation to display the two-dimensional
structure of the statement.
 
C

Carl Banks

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"
) \
:

Hmm. Complaints about the backslash come up from time to time, but this
is the first time I can recall anyone complaining because you couldn't
break a line right after the "for" keyword.

I'm sure you'll find agreement that backslash continuations are ugly, but
probably hardly anyone feels compelled to use them the way you
demonstrate above.


[snippage]

No offense, but using things like "#end for" isn't really the best way to
impress (most of) the Python community with your eye for syntax.

You're relatively new to Python, correct? If so, maybe you should give
yourself some time to see if some of Python's unconventional features
grow on you. I've found that wherever Python differs from the common
ways to do things, it's usually different in a good way. You should give
Python a chance to be Python, without trying to force it to be like
languages that do things like break for statements into several lines, or
use block delimiters.


Carl Banks
 
S

Steven D'Aprano

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.
 
B

Bjoern Schliessmann

Lawrence said:
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

IMHO, that's no Python syntax wart, but a coding style wart.

What's wrong with this:

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"]
)
)

It's still quite crammed; I think I'd write it more explicit.

(Personally, I've never needed the possibility to tear a "for" apart
so it covers more than ten lines, or to waste lines by placing
single parentheses or colons in them -- my screen really isn't
unlimited in size.)

Regards,


Björn


--
BOFH excuse #247:

Due to Federal Budget problems we have been forced to cut back on
the number of users able to access the system at one time. (namely
none allowed....)
 
D

Duncan Booth

Steven D'Aprano said:
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):

Or even just:

records = GetEachRecord("lots", "and", "lots", "of", "arguments")
for link in records:
...
 
S

Stefan Behnel

Lawrence said:
But then you can no longer use indentation to display the two-dimensional
structure of the statement.

Not sure what you mean here, but most people wouldn't write such code anyway.

You could easily split it up as

def format_record(record):
return "<P..." % (
record[...],
record[...])

link_records = GetEachRecord(
...
)

for record in link_records:
out.write(format_record(record))

Which reads much better IMHO.

Stefan
 
B

Bjoern Schliessmann

Lawrence said:
But then you can no longer use indentation to display the
two-dimensional structure of the statement.

How can a statement be two-dimensional? Like a two-dimensional
Turing Machine?

Regards,


Björn
 
W

Wildemar Wildenburger

Steven said:
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?
Boy did that make me laugh! The notion of a "code uglifier" just is a
pearl. (I hate to call attention to these things, but that was actually
a pun. I'm just too proud of it.)

/W
 
J

James Stroud

Lawrence said:
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


I usually write my code in a way that can be understood by looking at
it, with self-documenting names, clear organization, and lines that fit
under 72 characters (if I can help it). But if you insist on making perl
noise, go 'head.


record_type = "links"
episodes = ("from_episode",)
format = "to_episodes = %s"
ids = [EpisodeID]
order = "order by when_created"

records = GetEachRecord(record_type, episodes, format, ids, order)

for Link in records:
template = "<P><A HREF=\"%s\">Back to episode %d</A>\n"
the_link = Link["from_episode"]
target = LinkToMe({"ep" : Link["from_episode"]})

msg = template % (target, the_link)

out.write(msg)


James
 
S

stef mientki

James said:
Lawrence said:
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


I usually write my code in a way that can be understood by looking at
it, with self-documenting names, clear organization, and lines that fit
under 72 characters (if I can help it). But if you insist on making perl
noise, go 'head.
Interesting thread, in automatically converting another language to Python,
the brackets idea might come in very handy.

"Self-documenting names",
yes I'm used to that too, but Python itself doesn't promote that ...
.... as "case-sensitive" seems to me a contradiction with
"self-documenting-names" ;-)

cheers,
Stef Mientki
 
L

Lawrence D'Oliveiro

How can a statement be two-dimensional?

Like this (from C++ code, but the idea is the same):

if
(
ThisCh >= 'A' and ThisCh <= 'Z'
or
ThisCh >= '0' and ThisCh <= '9'
or
ThisCh == '_'
or
ThisCh == '.'
)
...
 
T

TheFlyingDutchman

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).
 
M

Marc 'BlackJack' Rintsch

Like this (from C++ code, but the idea is the same):

if
(
ThisCh >= 'A' and ThisCh <= 'Z'
or
ThisCh >= '0' and ThisCh <= '9'
or
ThisCh == '_'
or
ThisCh == '.'
)
...

I still down see the second dimension. If you say 2D I think of
something like a table with two columns and not a sequential condition
spread over several lines.

Ciao,
Marc 'BlackJack' Rintsch
 
L

Lawrence D'Oliveiro

Marc 'BlackJack' Rintsch said:
I still down see the second dimension.

Horizontal + vertical = 2 dimensions.

A more complicated example:

if
(
TheProduct == JobSettings.Product.end()
or
TheProduct->second.PerType != ProductPerPerson
and
TheProduct->second.PerType != ProductPerNone
)
...
 
M

Marc 'BlackJack' Rintsch

Horizontal + vertical = 2 dimensions.

Wow I wrote two dimensional programs all the time without knowing it.
A more complicated example:

if
(
TheProduct == JobSettings.Product.end()
or
TheProduct->second.PerType != ProductPerPerson
and
TheProduct->second.PerType != ProductPerNone
)
...

Sorry I still don't understand how this could be called a 2D statement.
Or is this already 3D!? I see a tree structure here, but still no table.
And this is also easily written that way in Python if you don't insist on
the line break after the ``if`` or can live with backslashes.

Ciao,
Marc 'BlackJack' Rintsch
 
L

Lawrence D'Oliveiro

Marc 'BlackJack' Rintsch said:
I see a tree structure here ...

Good, you're improving.
... but still no table.

Who said anything about a table?
And this is also easily written that way in Python if you don't insist on
the line break after the ``if`` or can live with backslashes.

Which is precisely the point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top