I think this looks great, I can't think of anything wrong with it.

Could we put this into the standard library, so that IDEs and linters are
programmed to recognise it?

On Wed, May 6, 2020 at 4:55 PM Joao S. O. Bueno <jsbu...@python.org.br>
wrote:

> Here -  with the current inspect.Signature, it is straightforward
> to get a decorator that can do that covering most, if not all,
> corner cases, and even adding some extra functionality:
>
> https://gist.github.com/jsbueno/f689a181d50384f627b43b9b2aabe4f2
>
> ```
>
> from inspect import signature, Parameter
> from functools import wraps, partial
>
> def autoassign(func=None, *, expand_kwargs=False):
>
>     if not func:
>         return partial(autoassign, expand_kwargs=expand_kwargs)
>     sig = signature(func)
>     @wraps(func)
>     def wrapper(*args, **kwargs):
>         instance = args[0]
>         bound_args = sig.bind(*args, **kwargs)
>         bound_args.apply_defaults()
>         for i, (key, value) in enumerate(bound_args.arguments.items()):
>             if i == 0 or sig.parameters[key].kind ==
> Parameter.POSITIONAL_ONLY:
>                 continue
>             if expand_kwargs and sig.parameters[key].kind ==
> Parameter.VAR_KEYWORD:
>                 for kwkey, kwvalue in value.items():
>                     setattr(instance, kwkey, kwvalue)
>                 continue
>             setattr(instance, key, value)
>         return func(*args, **kwargs)
>     return wrapper
>
> """
> class A:
>     @autoassign
>     def __init__(self, a, b, c=3):
>         pass
>
> a = A(1, 2)
> assert a.a == 1 and a.b == 2 and a.c == 3
> """
>
>
>
> ```
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/65RKVROC6NKPI3DH2V7Y23D4MYKVIHS7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to