2014-02-10 98 views
0

有人可以澄清为什么这会产生一个名称错误,其中类A无法找到decorator_warehouse变量,即使它位于其父类Mixin中?继承和装饰器

NameError: name 'decorator_warehouse' is not defined 

另外,我怎样才能得到所需的功能是容纳不同的类比持有我想装饰功能的类内的decorator_warehouse。

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() 
+0

的可能重复的[Python的装饰是基类的部分不能被用于装饰在继承的类的成员函数](HTTP://计算器。 COM /问题/ 3782040 /蟒-装饰 - 即-是部分对的一个碱基类不能待用于到装饰-membe) –

回答