On Wed, Oct 24, 2018 at 4:41 PM Steven D'Aprano <steve@pearwood.info> wrote:
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?

The point is not saving a line or typing, but saving a thought. Expressing the intent of the factory function more clearly.

Decorators don't do more than "saving one line", either.

But the biggest reason I'd like something like this is that it solves a *specific* version of the multi-line anonymous function that comes up over and over and over again, and maybe by chipping away at those use-cases we can stop seeing *that* debate over and over and over again. :-)

So, no, it doesn't save a lot of typing, but I'm never interested in that. I don't spend a lot of time typing code, I spend it reading code, and something like this would certainly help there, imho.
Python-ideas mailing list
Code of Conduct: http://python.org/psf/codeofconduct/