如何将类字段作为参数传递给类方法上的装饰器?我想做的是:
class Client(object):
def __init__(self, url):
self.url = url
@check_authorization("some_attr", self.url)
def get(self):
do_work()
它抱怨自我不存在是为了传递自我。指向装饰器的Url。有办法解决这个问题吗?
如何将类字段作为参数传递给类方法上的装饰器?我想做的是:
class Client(object):
def __init__(self, url):
self.url = url
@check_authorization("some_attr", self.url)
def get(self):
do_work()
它抱怨自我不存在是为了传递自我。指向装饰器的Url。有办法解决这个问题吗?
当前回答
我知道这个问题已经很老了,但是下面的解决方法以前没有人提过。这里的问题是你不能在类块中访问self,但你可以在类方法中访问。
让我们创建一个虚拟装饰器来多次重复一个函数。
import functools
def repeat(num_rep):
def decorator_repeat(func):
@functools.wraps(func)
def wrapper_repeat(*args, **kwargs):
for _ in range(num_rep):
value = func(*args, **kwargs)
return
return wrapper_repeat
return decorator_repeat
class A:
def __init__(self, times, name):
self.times = times
self.name = name
def get_name(self):
@repeat(num_rep=self.times)
def _get_name():
print(f'Hi {self.name}')
_get_name()
其他回答
我知道这个问题已经很老了,但是下面的解决方法以前没有人提过。这里的问题是你不能在类块中访问self,但你可以在类方法中访问。
让我们创建一个虚拟装饰器来多次重复一个函数。
import functools
def repeat(num_rep):
def decorator_repeat(func):
@functools.wraps(func)
def wrapper_repeat(*args, **kwargs):
for _ in range(num_rep):
value = func(*args, **kwargs)
return
return wrapper_repeat
return decorator_repeat
class A:
def __init__(self, times, name):
self.times = times
self.name = name
def get_name(self):
@repeat(num_rep=self.times)
def _get_name():
print(f'Hi {self.name}')
_get_name()
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()
输出: My_rule2:好的
是的。不是在类定义时传入实例属性,而是在运行时检查它:
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
装饰器截取方法参数;第一个参数是实例,因此它从实例中读取属性。如果你不想硬编码属性名,你可以将属性名作为字符串传递给装饰器,并使用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
我知道这是一个老问题,但这个解决方案还没有被提及,希望它可以帮助到今天的人,8年后。
那么,如何包装包装?让我们假设不能改变装饰器,也不能装饰init中的这些方法(它们可能是@property decoration或其他)。总是有可能创建自定义的、特定于类的装饰器,该装饰器将捕获self并随后调用原始的装饰器,并将运行时属性传递给它。
下面是一个工作示例(f-string需要python 3.6):
import functools
# imagine this is at some different place and cannot be changed
def check_authorization(some_attr, url):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(f"checking authorization for '{url}'...")
return func(*args, **kwargs)
return wrapper
return decorator
# another dummy function to make the example work
def do_work():
print("work is done...")
###################
# wrapped wrapper #
###################
def custom_check_authorization(some_attr):
def decorator(func):
# assuming this will be used only on this particular class
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
# get url
url = self.url
# decorate function with original decorator, pass url
return check_authorization(some_attr, url)(func)(self, *args, **kwargs)
return wrapper
return decorator
#############################
# original example, updated #
#############################
class Client(object):
def __init__(self, url):
self.url = url
@custom_check_authorization("some_attr")
def get(self):
do_work()
# create object
client = Client(r"https://stackoverflow.com/questions/11731136/class-method-decorator-with-self-arguments")
# call decorated function
client.get()
输出:
checking authorisation for 'https://stackoverflow.com/questions/11731136/class-method-decorator-with-self-arguments'...
work is done...
一个更简洁的例子如下:
#/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!