Re:Python indentation: very nice explanation for the curious...
by andyn on Friday September 21, 2007 @10:32AM
(#57945)
Python 2.5 now lets you do:
x = f(x) if a else g(y)
f() and g() aren't evaluated till they're needed.
Re:Python indentation: very nice explanation for the curious...
by brlewis on Friday September 21, 2007 @11:30AM
(#57962)
That syntax looks like it's intended to communicate that f(x) is the normal result, and g(y) is exceptional. I wouldn't want to use it unless that's what I intended to communicate. It also looks like you would need to put parens around the "if" expression if you were to use it within a more complex expression, e.g
2 + (f(x) if a else g(y)) + 3
Re:Python indentation: very nice explanation for the curious...
by nostrademons on Friday September 21, 2007 @11:47AM
(#57968)
And before Python 2.5, people usually used this idiom:
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.
Re:Python indentation: very nice explanation for the curious...
by brlewis on Friday September 21, 2007 @01:32PM
(#58005)
I don't think the keywords are nonsensical.
(or (and a (f x))
(g y))
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 you wanted it to return a value, you'd have to rewrite 'if' as a function and say something like: 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')