Python: Jak wywołać metodę instancji z metody klasy tej samej klasy
Mam klasę w następujący sposób:
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)
i chciałbym móc:
>>> obj = MyClass.new()
>>> obj.int # must be defined
9
Mam na myśli bez tworzenia nowej instancjiMyClass
, ale oczywiście nie jest to takie proste, ponieważ dzwonienieMyClass.new()
rzucaTypeError: unbound method get_params() must be called with MyClass instance as first argument (got nothing instead)
Czy jest jakiś sposób, aby to osiągnąć? Dzięki