add a decorate function to a class

Posted by wiso on Stack Overflow See other posts from Stack Overflow or by wiso
Published on 2010-03-23T16:05:54Z Indexed on 2010/03/23 17:33 UTC
Read the original article Hit count: 239

Filed under:
|
|

I have a decorated function (simplified version):

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...

now I want to add this method to a pre-esisting class.

ROOT.TChain.DrawPlot = _DrawPlot

when I call this method:

chain = TChain()
chain.DrawPlot(opts)

I got:

self.memoized[hash] = self.function(*args)
TypeError: _DrawPlot() takes exactly 2 arguments (1 given)

why doesn't it propagate self?

© Stack Overflow or respective owner

Related posts about python

Related posts about decorators