2024-05-16 21:07:17 发布
网友
如何将类字段作为参数传递给类方法上的装饰器?我想做的是:
class Client(object): def __init__(self, url): self.url = url @check_authorization("some_attr", self.url) def get(self): do_work()
它抱怨self不存在,无法将self.url传递给decorator。有办法解决这个问题吗?
self.url
from re import search from functools import wraps def is_match(_lambda, pattern): def wrapper(f): @wraps(f) def wrapped(self, *f_args, **f_kwargs): if callable(_lambda) and search(pattern, (_lambda(self) or '')): f(self, *f_args, **f_kwargs) return wrapped return wrapper class MyTest(object): def __init__(self): self.name = 'foo' self.surname = 'bar' @is_match(lambda x: x.name, 'foo') @is_match(lambda x: x.surname, 'foo') def my_rule(self): print 'my_rule : ok' @is_match(lambda x: x.name, 'foo') @is_match(lambda x: x.surname, 'bar') def my_rule2(self): print 'my_rule2 : ok' test = MyTest() test.my_rule() test.my_rule2()
ouput: my_rule2 : ok
是的。不要在类定义时传入实例属性,而是在运行时检查它:
def check_authorization(f): def wrapper(*args): print args[0].url return f(*args) return wrapper class Client(object): def __init__(self, url): self.url = url @check_authorization def get(self): print 'get' >>> Client('http://www.google.com').get() http://www.google.com get
decorator拦截方法参数;第一个参数是实例,因此它从中读取属性。如果不想对属性名进行硬编码,可以将属性名作为字符串传递给decorator并使用getattr:
getattr
def check_authorization(attribute): def _check_authorization(f): def wrapper(self, *args): print getattr(self, attribute) return f(self, *args) return wrapper return _check_authorization
一个更简洁的例子如下:
#/usr/bin/env python3 from functools import wraps def wrapper(method): @wraps(method) def _impl(self, *method_args, **method_kwargs): method_output = method(self, *method_args, **method_kwargs) return method_output + "!" return _impl class Foo: @wrapper def bar(self, word): return word f = Foo() result = f.bar("kitty") print(result)
将打印:
kitty!
是的。不要在类定义时传入实例属性,而是在运行时检查它:
decorator拦截方法参数;第一个参数是实例,因此它从中读取属性。如果不想对属性名进行硬编码,可以将属性名作为字符串传递给decorator并使用
getattr
:一个更简洁的例子如下:
将打印:
相关问题 更多 >
编程相关推荐