The preceding section shows how you can use a Python tuple to combine two simple values into a compound value. In this case, we use a 2-element tuple whose first element is the snail's time and the second element is its name.
We might say that this tuple is an abstract data type, that is, a way of combining Python's basic types (such as floats and strings) into new combinations.
The next step is to combine values and functions into an abstract data type. Historically, this is how object-oriented programming arose. The “objects” are packages containing simpler values inside them. However, in general, these packages can also contain functions.
Before we start looking at how we build abstract data types in Python, let's define some import terms and look at some real-world examples.
classWhen we try to represent in our program some items out in the real world, we first look to see which items are similar, and group them into classes. A class is defined by one or more things that share the same qualities.
For example, we could define the class of fountains by saying that they are all permanent man-made structures, that they hold water, that they are outdoors in a public place, and that they keep the water in a decorative way.
It should be easy to determine whether any item is a member of the class or not, by applying these defining rules. For example, Trevi Fountain in Rome fits all the rules: it is man-made, holds water, is outdoors, and is decorative. Lake Geneva has water spraying out of it, but it's not man-made, so it's not a fountain.
instance
One of the members of a class. For example, the class of airplanes includes the Wright Biplane of 1903, and the Spirit of St. Louis that Charles Lindbergh flew across the Atlantic.
An instance is always a single item. “Boeing 747” is not an instance, it is a class. However, a specific Boeing 747, with a unique tail number like N1701, is an instance.
attribute
Since the purpose of most computer applications is in record-keeping, within a program, we must often track specific qualities of an instance, which we call attributes.
For example, attributes of an airplane include its wingspan, its manufacturer, and its current location, direction, and airspeed.
We can classify attributes into static and dynamic attributes, depending on whether they change or not. For example, the wingspan and model number of an airplane do not change, but its location and velocity can.
operations
Each class has characteristic operations that can be performed on instances of the class. For example, operations on airplanes include: manufacture; paint; take off; change course; land.
Here is a chart showing some classes, instances, attributes, and operations.
| Class | Instance | Attribute | Operation |
|---|---|---|---|
| Airplane | Wright Flyer | Wingspan | Take off |
| Mountain | Socorro Peak | Altitude | Erupt |
| Clock | Skeen Library Clock | Amount slow per day | Decorate |
You have now seen definitions for most of the important terms in object-oriented programming. Python classes and instances are very similar to these real-world classes and instances. Python instances have attributes too.
For historical reasons, the term method is the object-oriented programming equivalent of “operation.”
The term constructor method is the Python name for the operation that creates a new instance.
So what is an object? This term is used in two different ways:
An object is just an instance.
Object-oriented programming means programming with classes.