1
이 코드는 예상대로 작동하지만 메모리 누수가 발생할 수 있습니까?동적 클래스 특성 생성
class Method(object):
method = []
def call(self,method,*args,**kwargs):
kwargs.update({'access_token': self.access_token})
print method,args,kwargs
def __getattr__(self,name):
cp = Method()
cp.access_token = self.access_token
cp.method = self.method + [name]
return cp
def __call__(self,*args,**kwargs):
return self.call('.'.join(self.method),*args,**kwargs)
class Api(Method):
access_token = 'setups on init'
a=Api()
a.get()
a.set(data=1)
또는 메소드 인스턴스가 호출 된 후 가비지가 될까요?
'a.friends.get_related()'(최대 5 일 수 있음)를 호출해도? 모든 instares (a 제외) 청소해야합니까? – eri
@eri : 예, 인터프리터의 모든 참조가 일시적이며 적절히 삭제 되었기 때문에 가능합니다. 일반 파이썬 메소드는 래퍼 객체이기도합니다 ([descriptor howto] (http://docs.python.org/2/howto/descriptor.html) 참조). 호출 표현식에 메모리 누수가 있으면 파이썬이 죽었을 것입니다 몇 백만년 전 메모리 누출 문제가 발생했습니다. :-) –