Lesson 33. Customizing classes

 

After reading lesson 33, you’ll be able to

  • Add special Python methods to your classes
  • Use special operators such as +, -, /, and * on your classes

You’ve been working with classes defined in the Python language since you wrote your first Python program. The most basic type of objects in the Python language, called built-in types, allowed you to use special operators on these types. For example, you used the + operator between two numbers. You were able to use the [] operator to index into a string or a list. You were able to use the print() statement on any of these types of objects, as well as on lists and dictionaries.

Consider this
  • Name five operations you can do between integers.
  • Name one operation you can do between two strings.
  • Name one operation you can do between a string and an integer.

Answer:

  • +, -, *, /, %
  • +
  • *

Each of these operations is represented in shorthand using a symbol. However, the symbol is only a shorthand notation. Each operation is actually a method that you can define to work with an object of a specific type.

33.1. Overriding a special method

 
 
 
 

33.2. Overriding print() to work with your class

 
 

33.3. Behind the scenes

 
 
 
 

33.4. What can you do with classes?

 
 
 
 

Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage