[Python-ideas] Syntax: 'return: ...' expressions
ron3200 at gmail.com
Fri Jan 23 23:06:11 CET 2015
On 01/23/2015 08:56 AM, Nick Coghlan wrote:
> On 21 January 2015 at 11:16, Guido van Rossum<guido at python.org> wrote:
>> >So it's hard to say to what extent this was a conscious choice and to what
>> >extent it was just tradition. But there's nothing necessarily wrong with
>> >tradition (up to a point). I think it still makes sense that statements are
>> >laid out vertically while expressions are laid out horizontally. Come to
>> >think of it, mathematics uses a similar convention -- a formula is laid out
>> >(primarily) horizontally, while a sequence of formulas (like a proof or a
>> >set of axioms) is laid out vertically.
> I really like that "each statement is like a single step in a
> mathematical proof" analogy.
There are some other distinctions. When you consider these, you can see
how many language designers come to similar solutions.
Expressions evaluate in unique name spaces, while statements generally do
not. Consider "a + b"; it is evaluated in a private method after the values
a and b are passed to it.
Statements are used to mutate the current name space, while expressions
generally do not.
Statements can alter control flow, while expressions generally do not.
Having a clear distinction between expressions and statements makes reading
and understanding code much easier.
I believe Python follows most of these conventions in most places, and when
it doesn't, it's usually for a practical reason that are fairly obvious.
For example, an "or" expression is a bit of both.
Another example of how python chooses a practical alternative is we can
nest expressions instead of using "call" statements on separate lines and a
stack to hold the augments and return values. That is what python does in
the byte code so we don't have to do it in explicit statements.
If you factor out all expressions you get byte code. Or if you factor out
all statements you get something like lisp.
Of course this subject is definitely a very subjective one which relays on
agreeing on the general meaning of the above sentences. OR... YMMV.
More information about the Python-ideas