Python 继承与装饰
有人能为我解释一下为什么这会产生一个名称错误,其中类a找不到decorator\u warehouse变量,即使它位于其父类Mixin的内部Python 继承与装饰,python,python-2.7,inheritance,Python,Python 2.7,Inheritance,有人能为我解释一下为什么这会产生一个名称错误,其中类a找不到decorator\u warehouse变量,即使它位于其父类Mixin的内部 NameError: name 'decorator_warehouse' is not defined 此外,我如何才能获得所需的功能,即将decorator_仓库放在一个不同的类中,而不是包含我希望装饰的功能的类中 class DecoratorWarehouse(object): """Object hosts a large number
NameError: name 'decorator_warehouse' is not defined
此外,我如何才能获得所需的功能,即将decorator_仓库放在一个不同的类中,而不是包含我希望装饰的功能的类中
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()
在这里找到了答案:
可能重复的
class A(Mixin):
@Mixin.decorator_warehouse.custom_decorator_A
def doing_stuff(self):
print "Doing some stuff"