[Python-ideas] Syntax: 'return: ...' expressions

Chris Angelico rosuav at gmail.com
Wed Jan 7 03:28:34 CET 2015


On Wed, Jan 7, 2015 at 1:08 PM, Yawar Amin <yawar.amin at gmail.com> wrote:
> But I think I see what's happening here: statements are the top dogs in
> Python, and when Python wants a statement but only has an expression, it
> 'promotes' the expression into a statement by evaluating it, throwing
> away its value, and pretending nothing happened (i.e., that there was a
> 'pass' statement there). Thus defining a class can have the nonsensical
> effect of calling a function.

One type of statement is (via a couple of levels of indirection) the
expr_stmt, which contains an expression and (if I'm not misreading the
grammar) may or may not assign it to anything.

https://docs.python.org/3/reference/grammar.html

It's not that an expression gets "promoted", it's that one valid form
of statement is an expression with nothing around it. In C, the
definition is more like "an expression followed by a semicolon", but
in Python, you don't need any adornment, so it looks identical.

However, there is (as far as I know) no way in Python to wrap a
statement into an expression. Some languages have such a thing (eg a
lambda function syntax, which is an expression returning a function,
and in which statements are allowed), but I don't think Python does,
anywhere; all compound units that allow statements are themselves
statements.

ChrisA


More information about the Python-ideas mailing list