다음 코드 조각에 대해 혼동스러워합니다. 어떻게 작동합니까? 그것은 데코레이터이며 lazily 속성을 초기화하고 다음 요청에 캐시 된 속성을 사용합니다. 코드를 보면 항상 self.method를 호출하는 것 같습니다. 약간의 설명은 주변 처음 result = self.method(inst)
메소드를 호출 한 것 같습니다, 나에게다음 코드에서 속성 캐싱이 작동하는 방식은 무엇입니까?
class cached_property(object):
def __init__(self, method, name=None):
self.method = method
self.name = name or method.__name__
self.__doc__ = method.__doc__
def __get__(self, inst, cls):
if inst is None:
return self
result = self.method(inst)
setattr(inst, self.name, result)
return result
문맥에 대한 지식이 없으면이 코드는 무엇을 말하기가 어렵습니다 – Denis
모든 마법은'setattr (inst, self.name, result) '에 의해 수행됩니다. – mgilson