How to check whether a method exists in Python?

Check if class has such method?

hasattr(Dynamo, key) and callable(getattr(Dynamo, key))

or

hasattr(Dynamo, 'mymethod') and callable(getattr(Dynamo, 'mymethod'))

You can use self.__class__ instead of Dynamo


It's easier to ask forgiveness than to ask permission.

Don't check to see if a method exists. Don't waste a single line of code on "checking"

try:
    dyn.mymethod() # How to check whether this exists or not
    # Method exists and was used.  
except AttributeError:
    # Method does not exist; What now?

Tags:

Python

Methods