Attach attributes to an object.

Documentation

  1. attribute in the Python Glossary
  2. Class Definition Syntax and Class definitions
  3. The ultimate base class object
    1. object in the Python Glossary
    2. object in the built-in functions in the Python Standard Library
  4. Attribute references (the dot). See “data attributes” in Instance Objects.
  5. Built-in functions:
    1. hasattr
    2. setattr
    3. getattr

In Variable number, we saw one way of passing a variable number of arguments to a function. In this example, we’ll see another way of doing it. But the machinery that we begin to introduce here will go far beyond this purpose.

An object is a big value that can contain smaller values inside of it. Not every type of value can do this; for example, an int cannot contain a smaller int.

The data type of an object is called a class. For example, the object mark in line 33 of person.py is an object of the class Person in lines 9–10. person will therefore be able to contain smaller values inside of it. The name of a class conventionally begins with an uppercase letter (Person in lines 9 and 15); the name of an object of that class conventionally begins with a lowercase letter (mark in line 33, person in line 13).

The smaller values contained in an object are called the attributes of the object. The object mark is born without attributes because the class definition in lines 9–10 does not list any attributes. It only has the word pass, which in this context means “no attributes”.

We’ll talk about the object in line 9 when we do inheritance. In the meantime, see “Classes without an inheritance list” in Class definitions.

Line 34 puts an attribute into the object mark. The name of the attribute is lastName and the value of the attribute is the string "Meretzky".

The for loop in lines 39–40 loops through all the attributes of the object, just to show you what attributes the object has at this point. (See the dictionary __dict__.) In real life, you’d probably print just the individual attributes you’re interested in, e.g.,
print(mark.lastName).

person.py

Before:
lastName Meretzky
age 64
favoriteSongs ['Help!', 'Day Tripper', 'Piggies']

After:
lastName Meretzky
favoriteSongs ['Help!', 'Day Tripper', 'Piggies', 'Revolution']
hair black

Why would we want to attach attributes to an object? Well, it’s one way of grouping a set of variables into a family by giving them a common last name: mark.age, mark.hair, mark.favoriteSongs, etc. And now that they’re a family, they can be passed simultaneously to a function (line 42), returned simultaneously from a function, destroyed simultaneously (we haven’t talked about this yet), etc.

I’m making an object sound like a dictionary. After all, a dictionary can be passed to a function, returned from a function, etc. So why create an object instead of a dictionary? It’s because an object can have more than just variables belonging to it. See the next example.

Things to try

  1. If you don’t like the EAFP style of lines 21–24, you can change them to the following lines in LBYL style.
        if hasattr(person, "age"):
            del person.age
    
    There is a slight danger in the LBYL style if your program is multi-threaded: another thread might delete the age attribute at an instant between the if and the del.
  2. We were able to write line 34 because we knew when we were writing the program that we wanted to name the attribute lastName. Had we not known this, we could not have written the dot and the lastName in that line. How could we not know the name of the attribute when we’re writing the program? Well, the name of the attribute might come from the user’s input as the program is running. In that case, we’d have to create the attribute with the setattr function. Change line 34 from
    mark.lastName = "Meretzky"
    
    to
    attributeName = input("What do you want to name the attribute? ")
    setattr(mark, attributeName, "Meretzky")
    
    What do you want to name the attribute? apellido
    Before:
    apellido Meretzky
    age 64
    favoriteSongs ['Help!', 'Day Tripper', 'Piggies']
    
    After:
    apellido Meretzky
    favoriteSongs ['Help!', 'Day Tripper', 'Piggies', 'Revolution']
    hair black
    

    If we had written

    print(mark.lastName)
    
    after line 34, we’d have to change it to
    print(getattr(mark, attributeName))
    
  3. Loop through the attributes in lines 39 and 46 in alphabetical order of their names. See Do, a deer.