[英]how to define a decorator for class to decorate class method
I want add some fixed vars to the class method.我想在 class 方法中添加一些固定变量。 But I did not want to pass those vars every time when using it.但是我不想在每次使用时都传递这些变量。 I cannot decorate the class method directly, because my class is a child class and I cannot modify the parent class.我不能直接装饰 class 方法,因为我的 class 是一个子 class 并且我不能修改父 ZA2F2ED4F8EBC2CBB4C21A29 Here is what I want to try:这是我想尝试的:
def callable(o):
return hasattr(o, "__call__")
def trace(f):
def traced(*args, **kw):
"Print message before and after a function call."
print("Entering", f.__name__)
kw['new value'] = 'new value'
result = f(*args, **kw)
print("Leaving", f.__name__)
return result
return traced
def mtrace(cls):
for key, val in cls.__dict__.items():
if key.startswith("__") and key.endswith("__") or not callable(val):
continue
print('calling in mtrace', key)
setattr(cls, key, trace(val))
print("Wrapped", key)
return cls
class dull:
@classmethod
def method1(cls, **arg):
print("Method 1 called with arg", arg)
@mtrace
class myclass(dull):pass
myclass.method1(a='hello')
output is: output 是:
try class method
Method 1 called with arg {'a': 'hello'}
the new value is not add to the method1.新值不会添加到方法 1。 I am not quite understand how the class method is called by the python interpreter.我不太明白 python 解释器如何调用 class 方法。 Thanks a lot if anyone can help me.如果有人可以帮助我,非常感谢。
I try use super to redefine the parent class.我尝试使用 super 重新定义父 class。 It works as I want.它按我的意愿工作。 But I didnot use a decorator.但我没有使用装饰器。 But It doesn't look pretty.但它看起来并不漂亮。
class BaseRequest: # cannot modify this class
@classmethod
def http_get(cls, *vars, **kws):
print("Method 1 called with arg", kws)
class MyRequest(BaseRequest):
extra_vars = {}
@classmethod
def http_get(cls, *vars, **kws):
kws.update(cls.extra_vars)
super().http_get(*vars, **kws)
@classmethod
def set_extra_vars(cls, **kws):
cls.extra_vars.update(kws)
MyRequest.set_extra_vars(header='my hearders')
MyRequest.http_get(a='hello')
Out put Method 1 called with arg {'a': 'hello', 'header': 'my hearders'}使用 arg {'a': 'hello', 'header': 'my hearers'} 调用的输出方法 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.