에 장식 기능을 추가 I가 장식 된 기능 (간단 버전) :클래스
class Memoize:
def __init__(self, function):
self.function = function
self.memoized = {}
def __call__(self, *args, **kwds):
hash = args
try:
return self.memoized[hash]
except KeyError:
self.memoized[hash] = self.function(*args)
return self.memoized[hash]
@Memoize
def _DrawPlot(self, options):
do something...
가 지금은 전 esisting 클래스에이 방법을 추가 할.
chain = TChain()
chain.DrawPlot(opts)
는 내가 가진 :
self.memoized[hash] = self.function(*args)
TypeError: _DrawPlot() takes exactly 2 arguments (1 given)
왜 자기를 전파하지 않습니다 나는이 메소드를 호출
ROOT.TChain.DrawPlot = _DrawPlot
?
새로운 스타일의 클래스를 사용하도록 항상 객체 대신 상속합니다. –
문제에 대한 선택 사항이있는 경우 대문자로 된 첫 글자로 메소드의 이름을 지정하지 마십시오. '_draw_plot' (PEP 8에서 권장)이나'_drawPlot'과 같은 이름을 사용하십시오. –
내 대답은 오해의 소지가있어 절단이 발생했습니다. Mike에게 감사드립니다. –