class Reader:
def __init__(self):
pass
def fetch_page(self):
with open('/dev/blockingdevice/mypage.txt') as f:
return f.read()
def fetch_another_page(self):
with open('/dev/blockingdevice/another_mypage.txt') as f:
return f.read()
class Wrapper(Reader):
def __init__(self):
super().__init__()
def sanity_check(func):
def wrapper():
txt = func()
if 'banned_word' in txt:
raise Exception('Device has banned word on it!')
return wrapper
@sanity_check
<how to automatically put this decorator on each function of base class? >
w = Wrapper()
w.fetch_page()
w.fetch_another_page()
在Wrapper
类的实例?
最佳答案
如果使用 python3.6 或更高版本,您可以使用 __init_subclass__
完成此操作
简单实现:(对于真实的东西,您可能需要注册表和 functools.wraps
等):
class Reader:
def __init_subclass__(cls):
cls.fetch_page = cls.sanity_check(cls.fetch_page)
cls.fetch_another_page = cls.sanity_check(cls.fetch_another_page)
def fetch_page(self):
return 'banned_word'
def fetch_another_page(self):
return 'not a banned word'
class Wrapper(Reader):
def sanity_check(func):
def wrapper(*args, **kw):
txt = func(*args, **kw)
if 'banned_word' in txt:
raise Exception('Device has banned word on it!')
return txt
return wrapper
演示:
In [55]: w = Wrapper()
In [56]: w.fetch_another_page()
Out[56]: 'not a banned word'
In [57]: w.fetch_page()
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-57-4bb80bcb068e> in <module>()
----> 1 w.fetch_page()
...
Exception: Device has banned word on it!
编辑:如果你不能改 rebase 类,你可以继承并创建一个适配器类:
class Reader:
def fetch_page(self):
return 'banned_word'
def fetch_another_page(self):
return 'not a banned word'
class ReadAdapter(Reader):
def __init_subclass__(cls):
cls.fetch_page = cls.sanity_check(cls.fetch_page)
cls.fetch_another_page = cls.sanity_check(cls.fetch_another_page)
class Wrapper(ReadAdapter):
def sanity_check(func):
def wrapper(*args, **kw):
txt = func(*args, **kw)
if 'banned_word' in txt:
raise Exception('Device has banned word on it!')
return txt
return wrapper
应该提供相同的结果。
关于python - 如何装饰子类中的所有继承方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55013817/