Built-in Functions and Types

hasattr()

Check whether attribute lookup completes without AttributeError. This is a lookup operation, not a passive inventory check: a property or custom getter may run and even have side effects. An AttributeError raised inside a getter also yields False, whereas other exceptions propagate. It does not prove that the attribute is writable or that future lookups will succeed.

Formal Syntax & Structure

python syntaxpython
hasattr(object, name, /)

Parameters & Attributes

NameTypeDescription
objectobjectTarget of lookup.
namestrAttribute name.

Return Value

bool

True if lookup succeeds; False if it raises AttributeError.

Working Example

Example Snippetpython
class Device:    online = False
print(hasattr(Device(), "online"))print(hasattr(Device(), "serial"))
Python Reference