2014-02-10 102 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) –

回答