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

Yawar Amin yawar.amin at gmail.com
Wed Jan 7 03:08:46 CET 2015


On 2015-01-06 03:25, Andrew Barnert wrote:
> [...]
> I think you're missing another important point here: statements and
> expressions are different things. Blocks are made up of statements,
> not expressions, ...

Actually I can offer counter-examples to that:

  def p(x): print x
  class A: p("Hello!")
  class B: 1
  # Etc.

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.

But there's currently no way of going in the other direction, i.e.
demoting a statement to an expression. Which is what I was trying to do.
I _still_ think at least something like the following would work (in
terms of Python's grammar[1]):

  expr_expr: 'expr' ':' small_stmt (';' small_stmt)* [';' expr]

So e.g.:

  x = expr: import os; os.system("date")

I'll explore this further and see where it goes.

Regards,

Yawar

[1] https://docs.python.org/3/reference/grammar.html


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 834 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150106/780627c4/attachment.sig>


More information about the Python-ideas mailing list