Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีดึง List of Methods ใน Python Class

โค้ดต่อไปนี้พิมพ์ List of Methods ของ Class ที่กำหนด ดังนี้

ตัวอย่าง

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

<type 'classobj'>
[('__init__', <unbound method foo.__init__>), ('bar', <unbound method foo.bar>), ('baz', <unbound method foo.baz>)]