Python Decorators and inheritance

Posted by wheaties on Stack Overflow See other posts from Stack Overflow or by wheaties
Published on 2010-06-08T20:42:40Z Indexed on 2010/06/08 20:52 UTC
Read the original article Hit count: 286

Filed under:
|
|

Help a guy out. Can't seem to get a decorator to work with inheritance. Broke it down to the simplest little example in my scratch workspace. Still can't seem to get it working.

class bar(object):
    def __init__(self):
        self.val = 4
    def setVal(self,x):
        self.val = x
    def decor(self, func):
        def increment(self, x):
            return func( self, x ) + self.val
        return increment

class foo(bar):
    def __init__(self):
        bar.__init__(self)
    @decor
    def add(self, x):
        return x

Oops, name "decor" is not defined.

Okay, how about @bar.decor? TypeError: unbound method "decor" must be called with a bar instance as first argument (got function instance instead)

Ok, how about @self.decor? Name "self" is not defined.

Ok, how about @foo.decor?! Name "foo" is not defined.

AaaaAAaAaaaarrrrgggg... What am I doing wrong?

© Stack Overflow or respective owner

Related posts about python

Related posts about inheritance