Skip to content
Built-in Functions

hasattr()

Check whether an object has a named attribute.

hasattr(object, name)

hasattr() is a quick way to test for an attribute before trying to use it. It returns a boolean instead of the attribute value.

Parameters

NameDescription
objectThe object or class to inspect.
nameThe attribute name as a string.

Returns

True if the attribute exists; otherwise False.

Try it

Run it and change it
class User:
    name = "Ada"
print(hasattr(User, "age"))

Related entries