Python:获取类中除内置方法外的所有方法名

# -*- coding:utf-8 -*-


class Clazz:
    name = ""

    def getName(self):
        pass

    def getAge(self):
        pass


def get_methods(self):
    return (list(filter(lambda m: not m.startswith("_") and callable(getattr(self, m)),
                        dir(self))))


if __name__ == '__main__':
    print(dir(Clazz))
    """
    ['__class__', '__delattr__', '__dict__', 
    '__dir__', '__doc__', '__eq__', '__format__', 
    '__ge__', '__getattribute__', '__gt__', '__hash__', 
    '__init__', '__init_subclass__', '__le__', '__lt__', 
    '__module__', '__ne__', '__new__', '__reduce__', 
    '__reduce_ex__', '__repr__', '__setattr__', 
    '__sizeof__', '__str__', '__subclasshook__', 
    '__weakref__', 'getAge', 'getName', 'name']
    """
    print(get_methods(Clazz))
    # ['getAge', 'getName']
上一篇:完善算法技术 规范算法应用(文章结尾有彩蛋)


下一篇:Web Audio API DEMO