<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<br>
<div class="moz-cite-prefix">On 01/09/2013 10:21, Steven D'Aprano
wrote:<br>
</div>
<blockquote cite="mid:522306FC.3070202@pearwood.info" type="cite">On
01/09/13 18:31, Musical Notation wrote:
<br>
<blockquote type="cite">In Haskell, you can write:
<br>
<br>
let x=1
<br>
y=2
<br>
<br>
In Python, why you can't write:
<br>
<br>
if True: x=x+1
<br>
y=x
<br>
<br>
?
<br>
</blockquote>
<br>
Because allowing that does not let you do anything different or
new that you couldn't do before, it would not make code any
clearer or more understandable, and it would decrease the
readability of the code.
<br>
<br>
</blockquote>
If I can be forgiven for slightly changing the subject (sorry,
Musical Notation):<br>
<br>
Talking of unconventional ways of indenting:<br>
<br>
In Python, writing multiple code statements on a single line,
especially with a semi-colin, appears to be a taboo roughly on a par
with appearing naked in public. But I believe that there are times
when it is the clearest way of writing code, viz. when it makes
visually obvious a <b>pattern</b> in the code.<br>
Here is one example, not very different from some "real" code that I
wrote:<br>
<br>
def test(condition, a, b):<br>
if condition=='equals' : return a==b<br>
if condition=='is greater than' : return a>b<br>
if condition=='contains' : return b in a<br>
if condition=='starts with' : return a.startswith(b)<br>
etc.<br>
<br>
And here is some real code that I wrote (not worth explaining in
detail). I am sorry that it breaks another convention, having lines
longer than 80 characters - this happens not be inconvenient for me,
and was the best authentic <b>real</b> example I could find without
spending a long time searching:<br>
<br>
assert PN[0].isalpha() ; FirstPart = PN[0] ; PN =
PN[1:].lstrip(Seps) # Must be a letter<br>
if PN[0].isalpha() : FirstPart += PN[0] ; PN =
PN[1:].lstrip(Seps) # May be a second letter<br>
assert PN[0].isdigit() ; FirstPart += PN[0] ; PN =
PN[1:].lstrip(Seps) # Must be a digit<br>
if PN and PN[0].isalnum() : FirstPart += PN[0] ; PN =
PN[1:] # May be a letter or digit<br>
<br>
(These examples look best with the colons/semicolons/equals
signs/statements lined up vertically. They will probably look
ragged in an e-mail. They should look as intended if they are cut
and pasted into a (fixed-size font) editor.)<br>
<br>
Writing the code like this makes apparent:<br>
(1) There is a pattern to the code.<br>
(2) Where the pattern is not quite consistent. E.g. in my
second example the first line contains "FirstPart =", the other
lines contain "FirstPart +=". <b>Seeing</b> this is half-way to
understanding it.<br>
(3) The conceptual separation of the whole chunk of code from
what precedes and what follows it (which can be emphasised by
putting a blank line before and after it).<br>
<b>None</b> of this would be so apparent if the code were written
one statement per line. (Is 'statement' the correct technical
term? Please correct me.) There is also a minor advantage to
writing fewer lines of code - you can see more of the program in one
screenfull at a time.<br>
<br>
(And: that you may find a smarter way of rewriting these specific
examples is not really the point. In my younger days I might have
written:<br>
if wkday==0: return 'Monday'<br>
if wkday==1: return 'Tuesday'<br>
etc.<br>
Nowadays I would probably write something like<br>
return { 0 : 'Monday', 1 : 'Tuesday' ... etc. }[wkday]<br>
And you may have an even better way.<br>
Again - not really the point.<br>
)<br>
<br>
Best wishes,<br>
Rob Cliffe<br>
</body>
</html>