[Python-ideas] Return for assignment blocks

Steven D'Aprano steve at pearwood.info
Wed Oct 24 16:35:12 EDT 2018

On Wed, Oct 24, 2018 at 09:18:14AM -0400, Calvin Spealman wrote:
> I'd like to suggest what I think would be a simple addition to `def` and
> `class` blocks. I don't know if calling those "Assignment Blocks" is
> accurate, but I just mean to refer to block syntaxes that assign to a name.
> Anyway, I propose a combined return-def structure, and optionally also
> allowing a return-class version. Omitting the name would be allowable, as
> well.
> This would only apply to a `def` or `class` statement made as the last part
> of the function body, of course.
> def ignore_exc(exc_type):
>     return def (func):
>         @wraps(func)
>         return def (*args, **kwargs):
>             try:
>                 return func(*args, **kwargs)
>             except exc_type:
>                 pass

Your example is too complex for me this early in the morning -- I can't 
tell what it actually *does*, as it is obscured by what looks like a 
bunch of irrelevent code.

I *think* you are proposing the following syntax. Am I right?

return def (func):
    # body of func

which is equivalent to:

def func:
    # body of func
return func

And similar for classes:

return class (K):
    # body of K

being equivalent to:

class K:
    # body of K
return K

Is it intentional that your example function takes no arguments? If the 
function did, where would the parameter list go in your syntax?

Aside from saving one line, what is the purpose of this?


More information about the Python-ideas mailing list