我有一个类实例化了一堆类。我不希望这些类中的每一个都被修饰(出于测试的目的),所以我用包装器将它们包装在主类中,包装器反过来包装它们的所有方法。当我创建类的多个实例时,它的函数已经被包装,因为我使用了未实例化的对象来准备它。有没有什么好办法避免每次都重新包装它们?你知道吗
简化示例:
用户资源.py:
class UserResource(object):
def test(self):
pass
资源.py:
def resource_wrapper(resource_class):
def decorate(cls):
for attr in cls.__dict__:
if callable(getattr(cls, attr)) and not attr.startswith('_'):
setattr(cls, attr, _function_wrapper(getattr(cls, attr)))
return cls
return decorate(resource_class)
def _function_wrapper(fn):
from functools import wraps
@wraps(fn)
def wrapper(*args, **kwargs):
self = args[0]
arg = kwargs.pop('test', False)
print arg # See output further down
return fn(*args, **kwargs)
return wrapper
太长了,读不下去了,DR <强>一组未实例化类,用包装器包装,包括其所有函数。函数在每次运行时都会被包装。你知道吗运行此命令时,我看到以下行为:
r1 = resource_wrapper(UserResource)()
r1.test(test=True)
# True
r2 = resource_wrapper(UserResource)()
r2.test(test=True)
# True
# False
r3 = resource_wrapper(UserResource)()
r3.test(test=True)
# True
# False
# False
有没有办法只包装一次函数而不是每次包装一次?你知道吗
您应该看看如何实现模板模式。这将为您提供标头中的通用性,以及仅在派生中实现的变体。你知道吗
我无法从您的问题中提取更多信息,但也许策略模式可以提供解决方案。你知道吗
我通过改变操作实例化对象而不是类本身来修复它。你知道吗
如果将我的资源包装器更改为
for attr in dir(cls):
,我将cls
传递给self
函数包装器。你知道吗为清楚起见,这是示例代码的最终结果:
用户资源.py:
资源.py:
用法:
相关问题 更多 >
编程相关推荐