I'm trying to come up with a way to allow specification of any number of class attributes upon instantiation, very similar to a dictionary. Ideal use case:
>>> instance = BlankStruct(spam=0, eggs=1)
>>> instance.spam
0
>>> instance.eggs
1
where BlankStruct is defined as:
class BlankStruct(Specifiable):
@Specifiable.specifiable
def __init__(self, **kwargs):
pass
I was thinking of using a parent class decorator, but am lost in a mind-trip about whether to use instance methods, class methods or static methods (or possibly none of the above!). This is the best I've come up with so far, but the problem is that the attributes are applied to the class instead of the instance:
class Specifiable:
@classmethod
def specifiable(cls, constructor):
def constructor_wrapper(*args, **kwargs):
constructor(*args, **kwargs)
cls.set_attrs(**kwargs)
return constructor_wrapper
@classmethod
def set_attrs(cls, **kwargs):
for key in kwargs:
setattr(cls, key, kwargs[key])
How can I make such a parent class?
NOTE: Yes, I know what I'm trying to do is bad practice. But sometimes you just have to do what your boss tells you.