4

I'm trying to get all the user-defined methods name from a class, ex:

class MyClass:
    def __init__(self, param1, param2):
        pass

    def one_method(self):
        pass

    def __repr__(self):
        pass

    def __str__(self):
        pass

    def __my_method_no_2(self, param2):
        pass

    def _my_method_no_3(self):
        pass

so far I have come to the following approach:

import inspect 

[name for name, _ in inspect.getmembers(MyClass, inspect.isroutine)
 if name not in {'__init_subclass__', '__subclasshook__'} and 
 getattr(MyClass, name).__qualname__.startswith(MyClass.__name__)]

output:

['_MyClass__my_method_no_2',
 '__init__',
 '__repr__',
 '__str__',
 '_my_method_no_3',
 'one_method']

this is the expected output, but this looks "ugly" and not even sure if is the right approach

2 Answers 2

3

Got the idea from here, but using __dict__ instead of dir.

Every python objects has a __dict__ attribute containing names of all attributes. Relevant docs here

The idea is: get a list of attributes from __dict__ and filter out everything non-callable

[attr for attr in  MyClass.__dict__ if callable(getattr(MyClass, attr))]

Matches with your output

Sign up to request clarification or add additional context in comments.

Comments

2

In Python 3.x, you can use dir without inspect or any external libraries:

method_list = [func for func in dir(Foo) if callable(getattr(Foo, func))]

To handle every method that you newly define in your class and override in your class, Try this:

class MyClass:
    li = []
    def __init__(self, param1, param2):
        pass

    def one_method(self):
        pass

    def __repr__(self):
        pass

    def __str__(self):
        pass

    def __my_method_no_2(self, param2):
        pass

    def _my_method_no_3(self):
        pass


print([func for func in dir(MyClass) if callable(getattr(MyClass, func)) and func in MyClass.__dict__])

Outputs:

['_MyClass__my_method_no_2', '__init__', '__repr__', '__str__', '_my_method_no_3', 'one_method']


Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.