0

Can someone clarify for me why this generates a name error wherein class A cannot find the decorator_warehouse variable, even though it is located inside of its parent class, Mixin?

NameError: name 'decorator_warehouse' is not defined

Also, how can I get the desired functionality which is to house the decorator_warehouse inside of a different class than the class which holds the function I wish to decorate.

class DecoratorWarehouse(object):
    """Object hosts a large number of custom decorators"""
    def custom_decorator_A(self, func):
        def wrapper(*args, **kwargs):
            print "doing stuff before call"
            res = func(*args, **kwargs)
            print "doing stuff after call"
            return res
        return wrapper

class Mixin(object):
    decorator_warehouse = DecoratorWarehouse()


class A(Mixin):
    @decorator_warehouse.custom_decorator_A
    def doing_stuff(self):
        print "Doing some stuff"

a = A()

a.doing_stuff()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
jsexauer
  • 681
  • 2
  • 12
  • 22
  • possible duplicate of [Python decorators that are part of a base class cannot be used to decorate member functions in inherited classes](http://stackoverflow.com/questions/3782040/python-decorators-that-are-part-of-a-base-class-cannot-be-used-to-decorate-membe) – Martijn Pieters Feb 10 '14 at 08:13

1 Answers1

0

Found the answer here: Python decorators that are part of a base class cannot be used to decorate member functions in inherited classes

class A(Mixin):
    @Mixin.decorator_warehouse.custom_decorator_A
    def doing_stuff(self):
        print "Doing some stuff"
Community
  • 1
  • 1
jsexauer
  • 681
  • 2
  • 12
  • 22