Statements as expressions [was Re: Undefined behaviour in C]

Steven D'Aprano steve at pearwood.info
Sun Mar 27 20:54:16 EDT 2016


On Mon, 28 Mar 2016 03:58 am, BartC wrote:

>> One of Guido's principles in designing Python was to keep it simple,
>> even where that might mean people could make errors with it.  This part
>> of the language is no different: any expression can be a statement.
> 
> Yeah, but even simpler would be that any statement can also be an
> expression! He didn't go that far though.

People say that, but I don't believe it. What does it mean to say that any
statement could also be an expression? If this statement is an expression:


if condition:
    print(1)
    print(2)
else:
    print(3)
    print(4)


what value should it return? Justify your choice.


What should be the return value of this statement?

while True:
    x += 1
    if condition: break


I don't think that "every statement is an expression" is conceptually
simpler at all. I think it is more difficult to understand. Nearly all
human languages make a distinction between things and actions:

* expressions return a value, which makes them a thing (noun);

* statements do something, which makes them an action (verb).


It's easy to understand the concept of an expression where the result is
dropped. "Hand me that hammer" and then you just drop the hammer on the
floor.

It's harder to understand the concept of functions (doing words, verbs) as
values. "First class functions" take a bit of mental effort to understand,
but the payoff it worth it.

But it is even harder to understand what it might mean for a while loop to
be a value, and the benefit of doing so seems significantly less than
compelling. Maybe it is easier for non-English speakers with quite
different linguistic models, but the benefit of adding statements as
expressions seems to me to be well less than the extra difficulty it would
produce.



-- 
Steven




More information about the Python-list mailing list