Get all methods of a given class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 14, 2022

banner

Photo from Unsplash

Get all methods of a given class in Python #

Use the inspect.getmembers() method to get all methods of a class, e.g. inspect.getmembers(Employee, predicate=inspect.isfunction). The getmembers() method will return a list containing all of the methods of the class.

main.py
import inspect class Employee(): def __init__(self, name, salary): self.salary = salary self.name = name def get_name(self): return self.name def get_salary(self): return self.salary # 👇️ called inspect.getmembers with the class itself list_of_methods = inspect.getmembers(Employee, predicate=inspect.isfunction) # 👇️ [('__init__', <function Employee.__init__ at 0x7f91845bdab0>), ('get_name', <function Employee.get_name at 0x7f9184696e60>), ('get_salary', <function Employee.get_salary at 0x7f9184696ef0>)] print(list_of_methods) # ------------------------------------------------------------------ bob = Employee('Bobbyhadz', 100) # 👇️ called inspect.getmembers with instance of the class list_of_methods = inspect.getmembers(bob, predicate=inspect.ismethod) # [('__init__', <bound method Employee.__init__ of <__main__.Employee object at 0x7fca7bac09d0>>), ('get_name', <bound method Employee.get_name of <__main__.Employee object at 0x7fca7bac09d0>>), ('get_salary', <bound method Employee.get_salary of <__main__.Employee object at 0x7fca7bac09d0>>)] print(list_of_methods)

We used the inspect.getmembers() method to get a list containing all of the methods of a class.

The inspect.getmembers method takes an object and returns all the members of the object in a list of tuples.

The first element in each tuple is the name of the member and the second is the value.

We set the predicate argument to inspect.function to get a list containing only the methods of the class.

The inspect.getmembers() method can also be passed an instance of a class, but you have to change the predicate to inspect.ismethod.

main.py
import inspect class Employee(): def __init__(self, name, salary): self.salary = salary self.name = name def get_name(self): return self.name def get_salary(self): return self.salary bob = Employee('Bobbyhadz', 100) list_of_methods = inspect.getmembers(bob, predicate=inspect.ismethod) # [('__init__', <bound method Employee.__init__ of <__main__.Employee object at 0x7fca7bac09d0>>), ('get_name', <bound method Employee.get_name of <__main__.Employee object at 0x7fca7bac09d0>>), ('get_salary', <bound method Employee.get_salary of <__main__.Employee object at 0x7fca7bac09d0>>)] print(list_of_methods)

The inspect.isfunction predicate returns True if the object is a Python function.

The inspect.ismethod predicate returns True if the object is a bound method written in Python.

Make sure to use inspect.isfunction when passing a class to theinspect.getmembers() method and inspect.ismethod when passing an instance to getmembers().

Alternatively, you can use the dir() function.

Get all methods of a given class using dir() #

To get all methods of a given class:

  1. Use the dir() function to get a list of the names of the class's attributes.
  2. Use a list comprehension to filter out the attributes that start with a double underscore and all non-methods.
  3. The list will only contain the class's methods.
main.py
class Employee(): def __init__(self, name, salary): self.salary = salary self.name = name def get_name(self): return self.name def get_salary(self): return self.salary class_methods = [method for method in dir(Employee) if not method.startswith('__') and callable(getattr(Employee, method)) ] print(class_methods) # 👉️ ['get_name', 'get_salary']

Filtering out the attributes that start with two underscores is optional.

Make sure to remove the condition if you need to keep method names that start with two underscores.

main.py
class_methods = [method for method in dir(Employee) if callable(getattr(Employee, method)) ] # ['__class__', '__delattr__', '__dir__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'get_name', 'get_salary'] print(class_methods)

The class_methods list contains all the method names of the class.

You can also use this approach to get all methods of an instance.

main.py
class Employee(): def __init__(self, name, salary): self.salary = salary self.name = name def get_name(self): return self.name def get_salary(self): return self.salary bob = Employee('Bobbyhadz', 100) class_methods = [method for method in dir(bob) if not method.startswith('__') and callable(getattr(bob, method)) ] print(class_methods) # 👉️ ['get_name', 'get_salary']

You can use the getattr function if you need to call some of the methods.

main.py
bob = Employee('Bobbyhadz', 100) class_methods = [method for method in dir(bob) if not method.startswith('__') and callable(getattr(bob, method)) ] print(class_methods) # 👉️ ['get_name', 'get_salary'] method_1 = getattr(bob, class_methods[0]) print(method_1()) # 👉️ Bobbyhadz

The getattr function returns the value of the provided attribute of the object.

The function takes the object, the name of the attribute and a default value for when the attribute doesn't exist on the object as parameters.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.