[英]List Decorated functions within Object
This is not the actual thing I'm doing but a similar scenario.这不是我正在做的实际事情,而是类似的场景。
I want to store a list of functions that have a certain decorator under the self.funcs
variable.我想在
self.funcs
变量下存储具有特定装饰器的函数列表。
class Calculator:
def __init__(self):
self.func = []
### Wrapper Function Here
cal = Calculator()
@<wrapper>
def add_function(no1,no2):
return no1+no2
print([i.__name__ for i in cal.funcs])
# Shows Functions
I don't understand your question clearly but I think you mean this eg我不明白你的问题,但我认为你的意思是这个,例如
class Foo:
def __init__(self):
self.funcs = ['a', 'b', 'c', 'd']
@foo
def a():
return None
@bar
def b():
return None
@car
def c():
return None
@bar
def d():
return None
Now assuming you have the list of wrappers you want.现在假设你有你想要的包装器列表。
desired_wrapper_to_search_for = [bar]
f = Foo()
method_names = f.funcs
your_result = []
for method in method_names:
for gwrapper in desired_wrapper_to_search_for:
if str(gwrapper.__code__.co_consts[2]) in str(eval(method)):
your_result.append(method)
Is this what you wanted?这是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.