[Python-ideas] Return for assignment blocks
Guido van Rossum
guido at python.org
Wed Oct 24 12:40:58 EDT 2018
On Wed, Oct 24, 2018 at 7:55 AM Rhodri James <rhodri at kynesim.co.uk> wrote:
> On 24/10/2018 15:04, Calvin Spealman wrote:
> > My idea is not "assignment blocks" those already exist. `def` and `class`
> > blocks are both syntaxes that assign to some name. I'm just using the
> term
> > to refer to them as a group.
> >
> > The proposal is just being able to return them. These two examples become
> > equivalent:
> >
> > def ignore_exc(exc_type):
> > return def (func):
> > @wraps(func)
> > return def (*args, **kwargs):
> > try:
> > return func(*args, **kwargs)
> > except exc_type:
> > pass
> >
> > def ignore_exc(exc_type):
> > def decorator(func):
> > @wraps(func)
> > def wrapped_func(*args, **kwargs):
> > try:
> > return func(*args, **kwargs)
> > except exc_type:
> > pass
> > return wrapped_func
> > return decorator
>
> Essentially this is a limited multi-line lambda. Either people are
> going to be surprised that you can only use it in a return statement or
> you have to open the whole can of worms about multi-line lambdas. Good
> luck on the latter.
>
Let's close that can quickly. Syntactically this is much simpler because
because there's no trouble with switching between expression-mode and
statement-mode. Also note that syntactically it is clearly a special form
of `def` statement -- it can even be decorated!
So let's review the proposal as a shorthand for defining a function and
immediately returning it. It saves one line plus picking a name. I
personally don't think that's enough of a benefit to warrant the extra
syntactic complexity (even if modest).
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181024/8ab05b2a/attachment.html>
More information about the Python-ideas
mailing list