Chapter 17 · pp. 183–192
Classes and Methods
Object-oriented programming (OOP) is a style of writing programs in which data and the operations on that data are grouped together. In this chapter, functions that work on a programmer-defined type are turned into methods that belong to that type's class. The change is mostly in syntax, but it makes the program's structure easier to see. (p. 183)
What makes a program object-oriented
Python is an object-oriented language, so it has features that support OOP. A program in this style has three defining traits (p. 183):
- It contains class definitions and method definitions.
- Most of the work is done by performing operations on objects.
- Objects often stand for things in the real world, and methods often match the ways those things behave or interact.
Here is an example. A Time class models the time of day as people record it. Functions that work with times, such as printing a time, model what people do with times. In the same way, Point and Rectangle classes model the mathematical ideas of a point and a rectangle. (p. 183)
Notice that earlier programs used classes like these but were not truly object-oriented. They kept the types and the functions that use them as separate pieces and did not state how the two are related. Turning those functions into methods makes the relationship explicit. (p. 183)
Why use special OOP features at all?
You do not strictly need these features. Most of them are another way to write things you could already do with ordinary functions. They are still worth using because the new syntax is often shorter and shows the program's structure more honestly. (p. 183)
For example, suppose a class definition is followed by a list of separate functions. Nothing in the code says that they belong together. You have to read each function and notice that every one takes a Time object as an argument. That observation is the reason methods exist. (pp. 183–184)
Methods versus functions
A method is a function that is associated with a particular class. You have already met methods on strings, lists, dictionaries and tuples. Now you can define your own for types you create. (p. 184)
Methods mean the same thing as functions, but they differ in two ways of writing them (p. 184):
- Where they are defined. A method is written inside the class definition. This makes the link between the class and the method clear.
- How they are called. A method is invoked with different syntax than a function call.
Converting a function to a method is a mechanical process: you follow a fixed series of steps. If you can convert in both directions, you can choose whichever form suits the job. (p. 184)
Worked example: printing a Time object
As an ordinary function, printing a time looks like this (p. 184):
class Time:
"""Represents the time of day."""
def print_time(time):
print('%.2d:%.2d:%.2d' % (time.hour, time.minute, time.second))
The format %.2d prints an integer using at least two digits, adding a leading zero if needed. To call the function, you pass a Time object as the argument:
>>> start = Time()
>>> start.hour = 9
>>> start.minute = 45
>>> start.second = 0
>>> print_time(start)
09:45:00
To make print_time a method, move its definition inside the class. The only change is the indentation, which now places the function under class Time: (p. 184):
class Time:
def print_time(time):
print('%.2d:%.2d:%.2d' % (time.hour, time.minute, time.second))
Two ways to call the method
Once print_time is inside the class, you can call it in two ways. (p. 184)
Function syntax (less common). Use dot notation with the class name, and pass the object as an ordinary argument:
>>> Time.print_time(start)
09:45:00
Here Time is the class name, print_time is the method name, and start is the argument. (p. 184)
Method syntax (shorter and more common). Put the object before the dot:
>>> start.print_time()
09:45:00
Both calls give the same result. In the second form, the object written before the dot is handed to the method as its first argument. This is why the call needs no parentheses content, even though the method definition has a parameter. (p. 184)
Key ideas to remember
- OOP organizes a program around classes, methods and operations on objects. (p. 183)
- Objects often model real things, and methods model what those things do. (p. 183)
- A method is a function tied to a class. It is defined inside the class and called with dot notation on an object. (p. 184)
- Switching from functions to methods does not change what the program computes. It makes the connection between a type and its operations clear. (pp. 183–184)
Check yourself
- List the three defining characteristics of object-oriented programming.
- What are the two syntactic differences between a method and a function?
- Why is a program that has a class and separate functions for it not really object-oriented?
- Given a
Time object named start and a method print_time defined in the class, write both ways to call it. Which one is more common?
- What single change turns the function
print_time into a method of Time?