hasattr()Easy Examples

Returns True if an object has the given named attribute

Basic hasattr() usage

Simple demonstration of the hasattr() built-in function.

python
class Dog:
    name = "Rex"
    def bark(self): pass

print(hasattr(Dog, "name"))
print(hasattr(Dog, "bark"))
print(hasattr(Dog, "fly"))

hasattr() is a built-in function that returns true if an object has the given named attribute.

hasattr() with different inputs

Calling hasattr() with various argument types.

python
# More hasattr() examples
print("hasattr() is a Python built-in function")
print(f"Type: {type(hasattr)}")

hasattr() accepts different types of arguments and produces corresponding results.

Want to try these examples interactively?

Open Easy Playground