i have classes of resources:
class tokens(): def __init__(self, a, b): self.a = self.b = b def create(self): return "tokens resource" class users(): def __init__(self, a, b): self.a = self.b = b def create(self): return "users resource" i have client class:
class client(): def __init__(self, account, password): self.account = account self.password = password def __getattr__(self, attr): self.__dict__[attr] = globals()[str.capitalize(attr)]("a", "b") using class want create client object lazy-loading attributes. unfortunately, exception first time. second time ok:
>>> client = client("account", "password") >>> print client.tokens.create() traceback (most recent call last): file "<stdin>", line 1, in <module> attributeerror: 'nonetype' object has no attribute 'create' >>> print client.tokens.create() tokens resource what doing wrong?
you never returning created attribute:
def __getattr__(self, attr): self.__dict__[attr] = globals()[str.capitalize(attr)]("a", "b") return self.__dict__[attr] you set attribute in self.__dict__, next attempt access finds attribute , never calls __getattr__ again.
i'm not sure why using str.capitalize() unobund method when can call on attr directly:
def __getattr__(self, attr): self.__dict__[attr] = globals()[attr.capitalize()]("a", "b") return self.__dict__[attr]
Comments
Post a Comment