Python indentation: very nice explanation for the curious or skeptical
secnetix.de
secnetix.de
-They remember pain of trying to find an error in a code, data, or config file that had significant whitespace. Consider the 'tab-delimited' spreadsheet format, for example. If you manually edit a tab-delimited list of numbers, and accidentally leave a space somewhere, the number might be read as a string.
-They've spent a lot of time dealing with HTML rendering, where whitespace gets mangled and cannot be trusted.
-They're afraid they won't be able to make their code readable.
-They're afraid that the whitespace rules will be complicated, requiring them to think constantly while adjusting to this newfangled way to delimit blocks.
It is also true that Python has survived already, but I doubt this concept would leak to new languages.
Upd: In other words, it's electronic typography that made the space character irrelevant.
Now if only there were some formatting standard to get rid of some of that parens in Lisp without loss of power.
If you scan over http://norvig.com/python-lisp.html, you'll see a lot of opportunities to remove parens; but in fact many of those are not semantically identical.
In python, a block does not return a value.
if a:f(x);else:g(y)
If you wanted it to return a value, you'd have to rewrite 'if' as a function and say something like: myif(a, [(f, x)], [(g, y)])
If you wanted to evolve python towards Lisp without having to add parens, I'd make it so indented blocks are actually expressions and automatically return a value to the defining block.(Of course, the normal python way is to use "return" statements or side-effects, rather than writing an ad-hoc version of the lisp 'if')
x = f(x) if a else g(y)
f() and g() aren't evaluated till they're needed. 2 + (f(x) if a else g(y)) + 3
x = a and f(x) or g(y)
If you know the idiom, I think this is more readable (conditional is on the left), though it has the drawback of using completely nonsensical keywords. (or (and a (f x))
(g y))
The white space issue doesn't really matter. Except for a few specialized areas like template creation, where Python is just not a natural fit.
That's one of the things I like about Rails - Ruby may not be the absolute perfect language for scripting web templates (maybe Tcl is?:-) but it's good enough, and it's useful without some of the weird hacks that the Python guys use to do templates.
% for row in rows:
* Here is a row: ${row}
% endfor
http://www.makotemplates.org/That's kind of 'hackish', but not bad at all if you ask me.
Compared to an ERB snippet it is about the same (though you'd probably use each and a block here in Ruby.
<% for row in row %>
* <%= row %>
<% end %>Anyway, I don't like the notation for programming, but I love it for HTML: http://haml.hamptoncatlin.com/