Attribute Error

Attributes in Python

I usually think about attributes as nouns that belong to an object. For example, “the student has two eyes”. But in Python, an attribute can also be an action that an object can perform—“The cat can jump”.

An attribute in Python means some property that is associated with a particular type of object. In other words, the attributes of a given object are the data and abilities that each object type inherently possesses. An object in Python is a simply an enclosed collection of these abilities and data, and is said to be of a specific type. Some examples of objects and their respective types in Python:

>>> aList = [] # an empty list-type object
>>> anInt = 3 # an integer-type object
>>> aDictionary = {} # an empty dictionary-type object

For each of these object types, there are a particular set of attributes or functionalities that belong to it. Let’s focus on some attributes that list type objects possess using the above example of an empty list.

Some examples of attributes belonging to list type objects are append, insert, remove, and sort. What this means is that for any list type object you create in Python, you can call any of these function attributes belonging to that object. For instance:

>>> aList = [] # This is an empty list.
>>> anotherList = [4, 3, 5, 8, 1]
Fig. 1: Before calling append

>>> aList.append(‘x’) # Put the string variable ‘x’ into the empty list.
>>> anotherList.sort() # Sort anotherList in ascending order.
Fig. 2: Making the call

>>> print aList # We have appended ‘x’ to the empty list.
[‘x’]
>>> print anotherList # We now have a sorted list.
[1, 3, 4, 5, 8]
Fig. 3: After calling append

As you can see, we call an attribute using the form object.attribute, where the period between the object and attribute means “access this object’s attribute”.

Note that though all of the attributes we've shown thus far have been function attributes, Python objects can also have data attributes. For example, a hypothetical Person object could have an attribute called age, which we would access in just the same way as before:

>>> print myRoomate.age
20

AttributeErrors in Python

Attribute errors in Python are generally raised when you try to access or call an attribute that a particular object type doesn’t possess. Using our prior example, we mentioned that list type objects have a particular set of attributes, including insert, remove, and sort, among others. This means that any list type object has these function attributes.

However, remember that not all objects necessarily have these attributes. This means that if we try to access the same attributes on a different object-type, we might cause Python to throw an error back at us. For example, if we try to use the append attribute of an integer, we will receive an AttributeError, because integer type objects do not possess that attribute:

>>> anInt = 8
>>> anInt.append(4) # Whoops!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'append'

Here we see that Python has returned an AttributeError. What it says is that our int type object “8” doesn’t have the ability to append. In PyLearn, the error message you’d see would look like the following:

line 2:
  anInt.append(4)

AttributeError: Integers cannot 'append'.

To see a list of all function and data attributes associated with an object of a given type, simply call the built-in function help() on an object of that type in an interactive Python session.

Or, if you want to see a fairly comprehensive overview of types and their attributes, check out the Python documentation on built-in types.