Python should allow a return value for some statements
or blocks such as `print` or `def`. This should not
always happen, but could certainly be useful.
This can be used instead of lambdas when something more
complex is required (the `def` example.)
What seems to me to be the most natural syntax is to
simply wrap the statement in parentheses. For example:
>>> foo = (print 'bar')
bar
>>> foo
'bar'
>>> f = (def f(x):
if x < 0:
return -5
return x ** 2 )
>>> f
<function f at 0x00A575B0>
>>> # A useful example:
>>> do_some_sort_of_logging( ( raise SomeSortOfError ) )
It will generally be obvious what should be returned,
but why not be more explicit? Here are some loose specs:
`def` would return the defined function.
`class` would return the defined class.
`print` would return the string representation of the
printed object.
`raise` will return the exception.
`import` returns the module or a tuple of modules.
This is not mean to be complete or a definitive syntax,
but I'd certainly like this discussed and something
along the lines of this proposal implemented.
|