Lesson 31. Creating a class for an object type

 

After reading lesson 31, you’ll be able to

  • Define a Python class
  • Define data properties for a class
  • Define operations for a class
  • Use a class to create objects of that type and perform operations

You can create your own types of objects to suit whatever your program needs. Except for atomic object types (int, float, bool), any object that you create is made up of other preexisting objects. As someone who implements a new object type, you get to define the properties that make up the object and the behaviors that you’ll allow an object to have (on its own or when interacting with other objects).

You usually define your own objects in order to have customized properties and behaviors, so that you can reuse them. In this lesson, you’ll view code you write from two points of view, just as when you wrote your own functions. You’ll separate yourself from a programmer/writer of a new object type and from the programmer/user of a newly created object type.

Before defining an object type by using a class, you should have a general idea of how you’ll implement it by answering two questions:

  • What is your object made up of (its characteristics, or properties)?
  • What do you want your object to do (its behaviors, or operations)?

31.1. Implementing a new object type by using a class

 

31.2. Data attributes as object properties

 
 
 
 

31.3. Methods as object operations and behaviors

 
 
 

31.4. Using an object type you defined

 
 

31.5. Creating a class with parameters in __init__

 
 
 

31.6. Dot notation on the class name, not on an object

 

Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage