Python Exception Class Hierarchy

Python Exception Handling – AttributeError

Moving along through our in-depth Python Exception Handling series, today we’ll dig into the AttributeError. The AttributeError in Python is raised when an invalid attribute reference is made, or when an attribute assignment fails. While most objects support attributes, those that do not will merely raise a TypeError when an attribute access attempt is made.

Throughout this article we’ll examine the AttributeError by looking at where it sits in the larger Python Exception Class Hierarchy. We’ll also discover a bit about how attributes and attribute references work in Python, then look at some functional sample code illustrating how to handle built-in and custom attribute access, and how doing so can raise AttributeErrors in your own code. Let’s get started!

The Technical Rundown

All Python exceptions inherit from the BaseException class, or extend from an inherited class therein. The full exception hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

As previously mentioned, the AttributeError is raised when attempting to access an invalid attribute of an object. The typically way to access an attribute is through an attribute reference syntax form, which is to separate the primary (the object instance) and the attribute identifier name with a period (.). For example, would attempt to retrieve the name attribute of the person object.

When evoking an attribute reference, under the hood Python expects to find the attribute that was directly accessed. If it fails to locate a matching attribute it will then call the __getattr__() method of the object, which performs a less efficient lookup of the instance attribute. If this also fails to find a matching attribute then an AttributeError is raised to indicate that an invalid attribute was accessed.

To illustrate with a code example we start with a modified Book class:

Notice that we’ve explicitly overriden the __getattr__(self, name: str) method of the base object class. This allows us to perform any custom logic that might be necessary when an attribute is not immediately located. To test this our test() method creates a new Book instance, outputs some explicit attributes to the log to prove everything works, and then attempts to directly access an invalid attribute of book.publisher:

Executing this code produces the following output:

As you can see, everything worked fine until our attempt to acccess the book.publisher attribute, at which point the overridden Book.__getattr__() method was invoked, in which we raised an AttributeError with the custom error message seen above.

However, there’s no reason to override the __getattr()__ method if custom logic isn’t required. To illustrate this we’ll temporarily comment out our Book.__getattr__() method so the built-in __getattr__() method is invoked instead. Rerunning our code now produces the following output with a slightly different AttributeError message:

Airbrake’s robust error monitoring software provides real-time error monitoring and automatic exception reporting for all your development projects. Airbrake’s state of the art web dashboard ensures you receive round-the-clock status updates on your application’s health and error rates. No matter what you’re working on, Airbrake easily integrates with all the most popular languages and frameworks. Plus, Airbrake makes it easy to customize exception parameters, while giving you complete control of the active error filter system, so you only gather the errors that matter most.

Check out Airbrake’s error monitoring software today and see for yourself why so many of the world’s best engineering teams use Airbrake to revolutionize their exception handling practices!