Abstract and concrete syntax
david.baelde at ens-lyon.fr
Thu Jun 9 03:32:12 CEST 2005
I tried python, and do like it. Easy to learn and read (at least for the
commonly used part), has a very large community so great doc and
contributions, and... the design is clean. I come from functional
programming languages, and I do like the proper static binding, the first
class functions. That's what interests me in Python. It's the most modern
of those addictive scripting languages.
But, there is something I can't believe. Statements are not expressions. I
tried to find some doc about that on the web. I'm not satisfied, and I'd
like to learn more and discuss here.
First, there are some trolls about Python Zen forbidding statements to be
expressions. But I think the Zen is often contradictory. I don't like
trolls, want something serious.
Basically, I found one good point against statements-as-expressions: it
cannot fit the indentation sensitive syntax.
Python allows statements inside suites and suites inside compound
statements. It also allows expressions inside statements and expressions
inside expressions. The one thing it never ever does is allow a suite or a
statement inside an expression, because doing so would utterly destroy the
handling of significant white space.
I feel there are some real problems here. But I can't find anything
absolutely unsolvable. I played a few examples, can't get anything
ambiguous. Maybe the nesting analyzer
(http://python.org/doc/2.4.1/ref/indentation.html) needs to take into
account parenthesis-like delimiters together with indentation, but that
doesn't look impossible (although I suspect it's harder than I can imagine).
The parenthesis trick could avoid over-deep indentation.
lambda x: (if a:
Well, I suspect there are some moral issues, and maybe pythonists don't
want statements in expressions. PEP #308 was about having a ternary (?:)
operator for expression level conditional, pythonists rejected it. I'd
like to read more about the motivations of the vote.
Many people will tell me that my tiny example can be written with the
if-else outside the assignation. I claim it's better to be able to express
it the way you mean it. That's why lambda is here for, too.
More generally, I think there is no abstract distinction between
statements and expressions. Everything is an expression, can be evaluated
to a value. Making function first class objects was the same kind of good
and beautiful idea. So if there is no abstract distinction, why should
there be a concrete one? If it's just a technical issue, let's work on it.
But I guess you'll have more to say on that subject...
More information about the Python-list