I have a class as follows:
class MyClass(object):
int = None
def __init__(self, *args, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def get_params(self):
return {'int': random.randint(0, 10)}
@classmethod
def new(cls):
params = cls.get_params()
return cls(**params)
and I would like to be able to do:
>>> obj = MyClass.new()
>>> obj.int # must be defined
9
I mean without creating a new instance of MyClass
, but obviously it's not that simple, because calling MyClass.new()
throws TypeError: unbound method get_params() must be called with MyClass instance as first argument (got nothing instead)
Is there any way to accomplish so?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…