Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-7drxs Total loading time: 0 Render date: 2024-07-22T05:24:18.057Z Has data issue: false hasContentIssue false

17 - Classes and Methods

Published online by Cambridge University Press:  05 June 2012

Allen B. Downey
Affiliation:
Olin College of Engineering, Massachusetts
Get access

Summary

OBJECT-ORIENTED FEATURES

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 Time class 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 Point and 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 that every function takes at least one Time object as an argument.

Type
Chapter
Information
Python for Software Design
How to Think Like a Computer Scientist
, pp. 189 - 200
Publisher: Cambridge University Press
Print publication year: 2009

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Classes and Methods
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.019
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Classes and Methods
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.019
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Classes and Methods
  • Allen B. Downey, Olin College of Engineering, Massachusetts
  • Book: Python for Software Design
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9780511813832.019
Available formats
×