In Python, how can you get the name of a member function's class?

后端 未结 5 474
小鲜肉
小鲜肉 2020-12-28 08:01

I have a function that takes another function as a parameter. If the function is a member of a class, I need to find the name of that class. E.g.

def analyse         


        
5条回答
  •  温柔的废话
    2020-12-28 09:02

    Please use following function to get method names inside of a class

    def getLocalMethods(clss):
    import types
    # This is a helper function for the test function below.
    # It returns a sorted list of the names of the methods
    # defined in a class. It's okay if you don't fully understand it!
    result = [ ]
    for var in clss.__dict__:
        val = clss.__dict__[var]
        if (isinstance(val, types.FunctionType)):
            result.append(var)
    return sorted(result)
    

提交回复
热议问题