Chapter 15. Classes and object-oriented programming

 

This chapter covers

  • Defining classes
  • Using instance variables and @property
  • Defining methods
  • Defining class variables and methods
  • Inheriting from other classes
  • Making variables and methods private
  • Inheriting from multiple classes

In this chapter, we discuss Python classes, which can be used in a manner analogous to C structures but which can also be used in a full object-oriented manner. For the benefit of readers who aren’t object-oriented programmers, we’ll discuss the use of classes as structures in the first two subsections.

The remainder of the chapter discusses OOP in Python. This is only a description of the constructs available in Python; it’s not an exposition on object-oriented programming itself.

15.1. Defining classes

A class in Python is effectively a data type. All the data types built into Python are classes, and Python gives you powerful tools to manipulate every aspect of a class’s behavior. You define a class with the class statement:

class MyClass:
    body

body is a list of Python statements, typically variable assignments and function definitions. No assignments or function definitions are required. The body can be just a single pass statement.

15.2. Instance variables

 

15.3. Methods

 
 
 

15.4. Class variables

 
 

15.5. Static methods and class methods

 
 
 

15.6. Inheritance

 
 

15.7. Inheritance with class and instance variables

 
 
 

15.8. Private variables and private methods

 

15.9. Using @property for more flexible instance variables

 
 
 
 

15.10. Scoping rules and namespaces for class instances

 
 
 

15.11. Destructors and memory management

 
 
 

15.12. Multiple inheritance

 
 

15.13. Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest