Chapter 17. Classes and Methods
Code examples from this chapter are available from http://thinkpython.com/code/Time2.py.
Python is an object-oriented programming language, which means that it provides features that support object-oriented programming.
It is not easy to define object-oriented programming, but we have already seen some of its characteristics:
Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.
Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.
For example, the
defined in Chapter 16 corresponds to the way people record
the time of day, and the functions we defined correspond to the kinds of
things people do with times. Similarly, the
Rectangle classes correspond to the
mathematical concepts of a point and a rectangle.
So far, we have not taken advantage of the features Python provides to support object-oriented programming. These features are not strictly necessary; most of them provide alternative syntax for things we have already done. But in many cases, the alternative is more concise and more accurately conveys the structure of the program.
For example, in the
Time program, there is no obvious connection between the class definition and the function definitions that follow. With some examination, it is apparent ...