Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Auto assign decorator - eliminates attribute assignment boilerplate in __init__ functions
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)
def wrapper(*args, **kwargs):
instance = args[0]
bound_args = sig.bind(*args, **kwargs)
for i, (key, value) in enumerate(bound_args.arguments.items()):
if i == 0 or sig.parameters[key].kind == Parameter.POSITIONAL_ONLY:
if expand_kwargs and sig.parameters[key].kind == Parameter.VAR_KEYWORD:
for kwkey, kwvalue in value.items():
setattr(instance, kwkey, kwvalue)
setattr(instance, key, value)
return func(*args, **kwargs)
return wrapper
class A:
def __init__(self, a, b, c=3):
a = A(1, 2)
assert a.a == 1 and a.b == 2 and a.c == 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment