Skip to content

How to Check If a Class Has a Specific Attribute in Python

Learn how to safely verify if a Python class or instance contains a specific attribute using hasattr() and getattr() to avoid AttributeError crashes.

Python is a dynamic language, meaning attributes can be added to objects or classes at runtime. However, attempting to access an attribute that does not exist results in an AttributeError. Whether you are working with dynamic data structures, plugins, or optional configurations, knowing how to safely check for the existence of an attribute is a fundamental skill.

This guide explains the difference between class and instance attributes and demonstrates two primary methods hasattr() and getattr() to handle attribute access safely without crashing your program.

Understanding the Problem: AttributeError

When you define a class, you typically define attributes in the __init__ method (instance attributes) or directly in the class body (class attributes). If you try to access an attribute that hasn't been defined, Python raises an error.

Scenario: let's define a simple Dog class.

class Dog:
    species = "Canis familiaris"  # Class Attribute

    def __init__(self, name, age):
        self.name = name          # Instance Attribute
        self.age = age            # Instance Attribute

dog1 = Dog("Buddy", 3)

try:
    print(f"Dog breed: {dog1.breed}")
except AttributeError as e:
    print(f"Error: {e}")

Output:

Error: 'Dog' object has no attribute 'breed'

Method 1: Using hasattr() (Boolean Check)

The hasattr() function allows you to "look before you leap." It takes two arguments: the object and the attribute name (as a string). It returns True if the attribute exists and False otherwise.

Use this method when you want to conditionally execute code based on the presence of an attribute.

class Dog:
    species = "Canis familiaris"  # Class Attribute

    def __init__(self, name, age):
        self.name = name          # Instance Attribute
        self.age = age            # Instance Attribute

dog1 = Dog("Buddy", 3)

if hasattr(dog1, "breed"):
    print(f"Breed: {dog1.breed}")
else:
    print("Breed attribute not found.")

if hasattr(dog1, "name"):
    print(f"Name: {dog1.name}")

Output:

Breed attribute not found.
Name: Buddy

Method 2: Using getattr() (Safe Access)

If your goal is to get the value of an attribute and provide a fallback if it's missing, getattr() is more concise and Pythonic than using an if hasattr: block.

getattr() takes three arguments:

  1. The object.
  2. The attribute name (string).
  3. A default value to return if the attribute is missing.
breed = getattr(dog1, "breed", "Unknown")
name = getattr(dog1, "name", "Unnamed")

print(f"Dog Name: {name}")
print(f"Dog Breed: {breed}")

Output:

Dog Name: Buddy
Dog Breed: Unknown

Class vs. Instance Attributes

It is important to distinguish where you are checking for attributes.

  • Checking the Instance (dog1): Checks instance attributes and class attributes (due to inheritance lookup).
  • Checking the Class (Dog): Checks only class attributes.
class Dog:
    species = "Canis familiaris"  # Class Attribute

    def __init__(self, name, age):
        self.name = name          # Instance Attribute
        self.age = age            # Instance Attribute

dog1 = Dog("Buddy", 3)

print(f"Does 'Dog' class have 'species'? {hasattr(Dog, 'species')}")
print(f"Does 'Dog' class have 'name'?    {hasattr(Dog, 'name')}")

print(f"Does 'dog1' instance have 'species'? {hasattr(dog1, 'species')}")
print(f"Does 'dog1' instance have 'name'?    {hasattr(dog1, 'name')}")

Output:

Does 'Dog' class have 'species'? True
Does 'Dog' class have 'name'?    False
Does 'dog1' instance have 'species'? True
Does 'dog1' instance have 'name'?    True

Conclusion

Handling missing attributes gracefully is key to writing robust Python code.

  1. Use hasattr(obj, 'attr') when you need a simple Boolean check for control flow logic.
  2. Use getattr(obj, 'attr', default) when you want to retrieve a value and provide a safe fallback for missing data.
  3. Understand the Scope: Remember that checking a Class object will not find attributes that belong only to specific instances.
Navigation