Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Given a Python object of any kind, is there an easy way to get the list of all methods that this object has?

Or,

if this is not possible, is there at least an easy way to check if it has a particular method other than simply checking if an error occurs when the method is called?

1 Answer

0 votes
by (106k points)

For a given Python object for getting the list of all methods that the object has you can use the below-mentioned code, which is replacing 'object' with the object that you want:

object_methods = [method_name for method_name in dir(object)

if callable(getattr(object, method_name))]

Another thing you can do is use the hasattr(object," method") method it will check for a particular method that you want.

Related questions

31k questions

32.4k answers

500 comments

693 users

Browse Categories

...