设计类时,我发现在类方法中,每次调用类方法时都会调用重复的步骤。例如,Python - 使用类方法来装饰其他类方法
class Queue(object):
def __init__(self):
self.connection = Connection()
def create(self, name):
self.probe = self.connection.plug()
self.probe.create(name)
self.probe.unplug()
def delete(self, name):
self.probe = self.connection.plug()
self.probe.delete(name)
self.probe.unplug()
而且有很多方法需要类似的步骤来'插入'和'拔掉''探头'。在这种设计中,我们需要在每次执行操作时“插入”并“拔下”“探针”。
因此,我正在考虑由装饰者包装这些函数,以使代码看起来不那么重复。
class Queue(object):
def __init__(self):
self.connection = Connection()
def _with_plug(self, fn):
def wrapper(*args, **kwargs):
self.probe = self.connection.plug()
fn(*args, **kwargs)
self.probe.unplug()
@_with_plug
def create(self, name):
self.probe.create(name)
@_with_plug
def delete(self, name):
self.probe.delete(name)
但是这种策略是行不通的。我怎么可以在类中使用方法来修饰其他方法来在调用方法之前和之后执行此类操作?
什么是“但这种策略是行不通的。”意思? – Carcigenicate
看起来像使用方法作为装饰器 - 以代码显示 - 总是给出错误,说明参数的数量。我认为这是我使用装饰者的方式不正确。 –