简体   繁体   中英

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM