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

Paul Rubin no.email at nospam.invalid
Sun Mar 27 21:40:23 EDT 2016


Steven D'Aprano <steve at pearwood.info> writes:
> if condition:
>     print(1)
>     print(2)
> else:
>     print(3)
>     print(4)
> what value should it return? Justify your choice.

It could whatever value that the last call to print() returns.  Lisp
has worked like that since the 1950's.

> What should be the return value of this statement?
>
> while True:
>     x += 1
>     if condition: break

It could return None, or break(val) could return val.

> I don't think that "every statement is an expression" is conceptually
> simpler at all. I think it is more difficult to understand. 

It hasn't been a problem in Lisp or its descendants, Erlang, Haskell,
etc.  I don't know about Ruby or Javascript.

> 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.

It means that you get to use an incredibly simple and beautiful
evaluation model.  Have you ever used Lisp or Scheme?  Give it a try
sometime.  Excellent free book: 
  https://mitpress.mit.edu/sicp/full-text/book/book.html



More information about the Python-list mailing list