Open In App

How Do I Get List Of Methods In A Python Class?

Last Updated : 13 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Effective Python programming and debugging depend on a grasp of the methods that are accessible in a class. You may investigate the features offered by a Python class by obtaining a list of its methods. The ideas and procedures required to get a list of methods within a Python class will be shown to you in this article. In this article, we will see how we can get a list of methods in a Python class.

Get a List Of Methods in a Python Class

Below are some of the examples by which we can get a list of methods in a Python class:

  • Using dir() Function
  • Using inspect Module
  • Using vars() Function
  • Using __dict__ Attribute

Using dir() Function

A list of names in the current local scope is the result of the dir() function. You may get a list of methods in the class by filtering out non-callable attributes and removing those that begin with “__” (dunder methods).

Python3




class MyClass:
    def method1(self):
        pass
 
    def method2(self):
        pass
 
    def method3(self):
        pass
 
 
# Get a list of methods using dir()
methods_list = [method for method in dir(MyClass) if callable(
    getattr(MyClass, method)) and not method.startswith("__")]
 
print("Methods using dir():", methods_list)


Output

Methods using dir(): ['method1', 'method2', 'method3']

Using inspect Module

With the help of the inspect.ismethod predicate and the getmembers() function from the inspect module, you may filter methods. A list of the class’s method names is returned by this.

Python3




import inspect
 
 
class MyClass:
    def method1(self):
        pass
 
    def method2(self):
        pass
 
    def method3(self):
        pass
 
 
# Get a list of methods using inspect module
methods_list = [method[0] for method in inspect.getmembers(
    MyClass, predicate=inspect.ismethod)]
 
print("Methods using inspect module:", methods_list)


Output

Methods using inspect module: []

Using vars() Function

The class attributes are contained in the __dict__ attribute of an object, which is returned by the vars() method. You may get a list of methods by eliminating attributes that begin with “__” and filtering out non-callable attributes.

Python3




class MyClass:
    def method1(self):
        pass
 
    def method2(self):
        pass
 
    def method3(self):
        pass
 
 
# Get a list of methods using vars()
methods_list = [method for method in vars(MyClass) if callable(
    getattr(MyClass, method)) and not method.startswith("__")]
 
print("Methods using vars():", methods_list)


Output

Methods using vars(): ['method1', 'method2', 'method3']

Using __dict__ Attribute

The class namespace is contained in the __dict__ property. You may get a list of methods by looping over it, eliminating non-callable characteristics, and removing those that begin with “__.”

Python3




class MyClass:
    def method1(self):
        pass
 
    def method2(self):
        pass
 
    def method3(self):
        pass
 
 
# Get a list of methods using __dict__ attribute
methods_list = [method for method in MyClass.__dict__ if callable(
    getattr(MyClass, method)) and not method.startswith("__")]
 
print("Methods using __dict__ attribute:", methods_list)


Output

Methods using __dict__ attribute: ['method1', 'method2', 'method3']



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads