Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set class attributes from default arguments

Tags:

python

I would like to automatically set default argument as class attributes. For example, I have (with more arguments, like a dozen or two) :

class Foo:
    def __init__(self,a=1,b=2,c=3,...):
        self.a = a
        self.b = b
        self.c = c
        ...

And I want define the attributes automatically, without having to rewrite self.x = x all the time in __init__ body.

I could use something like :

class Foo:
    def __init__(self, **kwargs):
        for attr, value in kwargs.items():
            setattr(self,attr,value)

But now I cannot give them default values. What I would like is some function that gives me a dictionary of the arguments with default values :

class Foo:
    def __init__(self,a=1,b=2,c=3,...,**kwargs):
        defargs = somefunction()

With defargs being {'a':1,'b':2,'c':3,...} (but not containing kwargs).

Closest thing I managed to do is :

class Foo:
    def __init__(self,a=1,b=2,c=3,...,**kwargs):
        defargs=locals()
        defargs.pop('self')
        defargs.pop('kwargs')
        for attr, value in defargs.items():
            setattr(self,attr,value)

But I don't know if there is not some potentially unsafe behavior in this code.

like image 274
Nihl Avatar asked Mar 16 '23 07:03

Nihl


1 Answers

In Python 3.3+ this can be done easily using inspect.signature:

import inspect


def auto_assign(func):
    signature = inspect.signature(func)

    def wrapper(*args, **kwargs):
        instance = args[0]
        bind = signature.bind(*args, **kwargs)
        for param in signature.parameters.values():
            if param.name != 'self':
                if param.name in bind.arguments:
                    setattr(instance, param.name, bind.arguments[param.name])
                if param.name not in bind.arguments and param.default is not param.empty:
                    setattr(instance, param.name, param.default)
        return func(*args, **kwargs)

    wrapper.__signature__ = signature # Restore the signature

    return wrapper


class Foo:
    @auto_assign
    def __init__(self, foo, bar, a=1, b=2, c=3):
        pass

f = Foo(10, 20)
g = Foo(100, 200, a=999)

print(f.__dict__)
print(g.__dict__)
print(inspect.getargspec(Foo.__init__))

Output:

{'bar': 20, 'b': 2, 'foo': 10, 'c': 3, 'a': 1}
{'bar': 200, 'b': 2, 'foo': 100, 'c': 3, 'a': 999}
ArgSpec(args=['self', 'foo', 'bar', 'a', 'b', 'c'], varargs=None, keywords=None, defaults=(1, 2, 3))
like image 136
Ashwini Chaudhary Avatar answered Mar 29 '23 00:03

Ashwini Chaudhary