Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách lấy danh sách method của Python class?

Code sau in danh sách method của class cho trước:

Ví dụ

class foo:
    def __init__(self):
        self.x = x
    def bar(self):
        pass
    def baz(self):
        pass
print(type(foo))
import inspect
print(inspect.getmembers(foo, predicate=inspect.ismethod))

Output

<class 'type'>
[('__init__', <function foo.__init__ at 0x...>), ('bar', <function foo.bar at 0x...>), ('baz', <function foo.baz at 0x...>)]