By now you have probably noticed that objects have a set of characteristics associated with them. These characteristics are collectively referred to as an object’s attributes. You can think of attributes as the things that make an object itself!
Still a bit confused? That’s ok! Let’s list all the attributes that might be found in a Vehicle object. In other words, what information do we need to make a vehicle object? In order to complete this thought exercise, you need to ask yourself what makes a vehicle, well, a vehicle? It’s important to keep in mind that the list of attributes you come up with will be based on your beliefs about the world. And that’s ok! Remember, computers are dumb. They will believe anything you tell them. ;p
I have included a drawing to help our discussion:
Soak it in. Try to list out all of the characteristics that the vehicles have in common. For example, I notice that all of our vehicles have some number of wheels. Can you think of anything else that they share?
Got your list? Here are some class attributes that make sense to me:
Some number of doors
Some number of wheels
Some number of passengers they can safely transport
I hope you noticed that my list of attributes is looking rather plain right now. In other words, there isn’t any specific information associated with the attributes in this list. What might it look like to describe these attributes of a vehicle object with Python?
# Attributes of a car object engine = True manufacturer = 'Honda' doors = 4 wheels = 4 passengers = 5
Surprise! We use variables to create the attributes of our objects. Furthermore, we can describe the state of our object by examining the values stored in these variables; the state of an object just refers to the information that is stored within its attributes at a given moment in time. We’ll talk about how we might change the state of an object in our next section.
What might the state of a different vehicle object look like?
# Attributes of a motorcycle object engine = True manufacturer = 'Honda' doors = 0 wheels = 2 passengers = 2
As you can see, it’s possible for our objects to look different from one another even though they are described by the same set of attributes! All you have to do is store different information in each attribute for each object created.