简体   繁体   中英

How to list all staticmethods of a python class

I have a class somewhat like following:

from joblib import Memory
import time


def find_static_methods(cls):
    # to be implemented
    pass


class A:

    def __init__(self, cache_path: str):
        self._memory = Memory(cache_path, verbose=0)
        self._methods = {}
        for name, method in find_static_methods(A):
            self._methods[name] = self._memory.cache(method)

    def __getattribute__(self, item):
        if item in self._methods:
            return self._methods[item]
        return super(A, self).__getattribute__(item)

    @staticmethod
    def method1(a: int, b: int):
        time.sleep(3)
        return a + b

I'm trying to memoize method1 using joblib.Memory . But I don't know the cache_path in advance. Please help me with the implementation of find_static_methods here.

Here is another way:

A_attrs = A.__dict__
for k, v in A_attrs.items():
    if isinstance(v, staticmethod):
        print(k)

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