Check if an Object has a specific Method in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. Check if an object has a specific Method in Python
  2. Check if an object has a specific Method using hasattr()
  3. Check if an object has a specific Method using try/except

# Check if an object has a specific Method in Python

To check if an object has a specific method:

  1. Use the getattr() function to get the attribute of the object.
  2. Use the callable() function to check if the attribute is a method.
  3. If the condition is met, the object has the specified method.
main.py
class Employee(): def __init__(self, first, last): self.first = first self.last = last def get_name(self): return f'{self.first} {self.last}' emp1 = Employee('bobby', 'hadz') method = getattr(emp1, 'get_name', None) if callable(method): print(method()) # 👉️ bobby hadz

check if object has specific method

The code for this article is available on GitHub

We used the getattr() function to get the get_name attribute of the object.

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.

We used None as the default value for when the attribute doesn't exist.

The last step is to use the callable() function to check if the attribute is a method.

The callable function takes an object as an argument and returns True if the object appears callable, otherwise False is returned.

If the specified attribute exists on the object and the attribute is callable, we can safely assume that it is a method.

Alternatively, you can use the hasattr() function.

# Check if an object has a specific Method using hasattr()

This is a three-step process:

  1. Use the hasattr() function to check if the attribute exists on the object.
  2. Use the callable() function to check if the attribute is callable.
  3. If both conditions are met, the object has the specified method.
main.py
class Employee(): def __init__(self, first, last): self.first = first self.last = last def get_name(self): return f'{self.first} {self.last}' emp1 = Employee('bobby', 'hadz') if hasattr(emp1, 'get_name') and callable(emp1.get_name): print(emp1.get_name()) # 👉️ bobby hadz

check if object has specific method using hasattr

The code for this article is available on GitHub

The hasattr function takes the following 2 parameters:

NameDescription
objectThe object we want to test for the existence of the attribute
nameThe name of the attribute to check for in the object
The hasattr() function returns True if the string is the name of one of the object's attributes, otherwise `False` is returned.

We used the boolean AND operator, so for the if block to run both conditions have to be met.

The object has to have the specified attribute and the attribute has to be callable.

# Check if an object has a specific Method using try/except

If you use a try/except statement, you would only be checking if the attribute exists on the object, not if it's callable.

main.py
class Employee(): def __init__(self, first, last): self.first = first self.last = last def get_name(self): return f'{self.first} {self.last}' emp1 = Employee('bobby', 'hadz') try: result = emp1.get_name() except AttributeError: print('The attribute does NOT exist')

check if object has method using try except

The code for this article is available on GitHub

If the get_name attribute doesn't exist on the object, an AttributeError is raised and the except block runs.

However, the attribute could exist on the object and not be callable, in which case a TypeError is raised.

The previous two approaches are more direct and explicit and do a better job at checking if an object has a specific method.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.