我有一个子类方法我想有条件地短路。我想要做的就是这样,除了我想把验证逻辑放到基类中。
class BaseClass(object):
def getvalue(self):
return True
def validate(self):
validated = self.getvalue()
return validated
class ExtendedClass1(BaseClass):
def do_some_work(self):
validated = self.validate()
if not validated:
print "Not validated."
return
print "Things are validated if the method got this far.", validated
class ExtendedClass2(BaseClass):
def do_some_work(self):
validated = self.validate()
if not validated:
print "Not validated."
return
print "Things are validated if the method got this far.", validated
class ExtendedClass3(BaseClass):
def do_some_work(self):
print "This one doesn't require validation."
work1 = ExtendedClass1()
work1.do_some_work()
work2 = ExtendedClass2()
work2.do_some_work()
work3 = ExtendedClass3()
work3.do_some_work()
按照this示例,我能够将一些重复的代码转换为装饰器模式。
class BaseClass(object):
def validate(input_function):
def wrapper(*args,**kwargs):
validated = True
if not validated:
print "Not validated."
return
input_function(*args, **kwargs)
return wrapper
validate = staticmethod(validate)
class ExtendedClass1(BaseClass):
@BaseClass.validate
def do_some_work(self):
print "Things are validated if the method got this far."
class ExtendedClass2(BaseClass):
@BaseClass.validate
def do_some_work(self):
print "Things are validated if the method got this far."
class ExtendedClass3(BaseClass):
def do_some_work(self):
print "This one doesn't require validation."
work1 = ExtendedClass1()
work1.do_some_work()
work2 = ExtendedClass2()
work2.do_some_work()
work3 = ExtendedClass3()
work3.do_some_work()
但是,我需要在装饰器中调用基类的方法来执行验证工作,并检索子类中的(validated)值。在这个例子here之后,我修改了装饰器,试图让它调用self.getvalue()。此时它不会出错,但它也不起作用,因为self.getvalue()不返回True。这开始看起来比它的价值更麻烦,但现在我很好奇它是否可能。
class BaseClass(object):
def getvalue(self):
return True
def validate(self):
def wrap(input_function):
def wrapper(*args,**kwargs):
validated = self.getvalue()
if not validated:
print "Not validated."
return
input_function(*args, **kwargs)
return wrapper
return wrap
validate = staticmethod(validate)
class ExtendedClass1(BaseClass):
@BaseClass.validate
def do_some_work(self):
print "Things are validated if the method got this far."#, validated
class ExtendedClass2(BaseClass):
@BaseClass.validate
def do_some_work(self):
print "Things are validated if the method got this far."#, validated
class ExtendedClass3(BaseClass):
def do_some_work(self):
print "This one doesn't require validation."
work1 = ExtendedClass1()
work1.do_some_work()
work2 = ExtendedClass2()
work2.do_some_work()
work3 = ExtendedClass3()
work3.do_some_work()
是否可以使用装饰器设置属性,然后再检索它?
...
self.validated = True
if not self.validated:
print "Not validated."
return
...
print work1.validated
...
AttributeError: 'ExtendedClass1' object has no attribute 'validated'
基本上,我想转此:
class ExtendedClass1(BaseClass):
def do_some_work(self):
validated = self.validate()
if not validated:
print "Not validated."
return
print "Things are validated if the method got this far.", validated
进入这个:
class ExtendedClass1(BaseClass):
@BaseClass.validate
def do_some_work(self):
print "Things are validated if the method got this far.", validated
使用Zaur Nasibov发布的建议,这个例子满足了我的用例。我仍然有兴趣知道@validate是否可以作为方法而不是独立函数实现,但这可以完成工作。
class BaseClass(object):
def getvalue(self):
return True
def validate(func):
def wrapped(self, *args, **kwargs):
validated = self.getvalue()
self.validated = validated
if not validated:
print "Not validated."
return
func(self, *args, **kwargs)
return wrapped
class ExtendedClass1(BaseClass):
@validate
def do_some_work(self,input):
print "Things are validated if the method got this far.", self.validated, input
class ExtendedClass2(BaseClass):
@validate
def do_some_work(self):
print "Things are validated if the method got this far.", self.validated
class ExtendedClass3(BaseClass):
def do_some_work(self):
print "This one doesn't require validation."#, self.validated
work1 = ExtendedClass1()
work1.do_some_work(input="some text")
work2 = ExtendedClass2()
work2.do_some_work()
work3 = ExtendedClass3()
work3.do_some_work()
答案 0 :(得分:1)
@tponthieux,您可以做的是设置被调用函数(方法)的属性,然后检索它:
简单示例(已更新):
def validate(func):
def wrapped(self, *args, **kwargs):
self.valid = True
func(self, *args, **kwargs)
return wrapped
class TestClass(object):
@validate
def do_some_work(self):
print "some work done"
tc = TestClass()
tc.do_some_work()
print tc.valid
答案 1 :(得分:1)
如果已经验证,只调用装饰方法怎么样?如果您愿意,可以传递validate
方法的返回值:
class BaseClass(object):
def getvalue(self):
return True
def validate(input_function):
def wrapper(self, *args, **kwargs):
self.validated = self.getvalue()
if not self.validated:
print "Not validated."
return
input_function(self, validated=self.validated, *args, **kwargs)
return wrapper
validate = staticmethod(validate)
class ExtendedClass1(BaseClass):
@BaseClass.validate
def do_some_work(self, validated=None):
print "Things are validated if the method got this far.", validated
class ExtendedClass2(BaseClass):
@BaseClass.validate
def do_some_work(self, validated=None):
print "Things are validated if the method got this far.", validated
class ExtendedClass3(BaseClass):
def do_some_work(self):
print "This one doesn't require validation."
work1 = ExtendedClass1()
work1.do_some_work()
work2 = ExtendedClass2()
work2.do_some_work()
work3 = ExtendedClass3()
work3.do_some_work()
此处的关键是将self
添加到wrapper
功能。发生的事情是你的装饰函数没有绑定到实例(并成为方法),但装饰器返回的函数(上例中的wrapper
)得到了绑定。因此,此函数将在调用时获取传入的self
(实例)参数!重要的是要记住@decorator
所做的只是简单地调用decorator
传递你正在装饰的函数,然后替换你装饰的函数与装饰器返回的内容。在您的示例中,这是wrapper
,对于类,在装饰之前,它与原始函数之间没有区别。
在上面的例子中,我明确声明了self
。如果我们没有,我们也可以从args
获取它:
def validate(input_function):
def wrapper(*args, **kwargs):
print "args[0] is now the instance (conventionally called 'self')", args[0]
self = args[0]
self.validated = self.getvalue()
if not self.validated:
print "Not validated."
return
input_function(validated=self.validated, *args, **kwargs)
另请注意,我们将一个额外的关键字参数传递给名为validated
的包装方法。这完全是可选的,您可以从示例中删除validated=self.validated
和validated=None
部分。