1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Object-Oriented Programming in Python

Connected

Exercise

Overloading +

Comparison operators aren't the only operators that can be overloaded. Classes in Python can implement custom functionality for arithmetic operators such as + and -. In this example, you'll practice overloading the + operator for a Storage class.

Instructions

100 XP
  • Define a magic method to overload the + operator for the Storage class.
  • Create a new Storage object called total_storage with a capacity that equals the combined capacity of both objects.
  • Add the onboard_storage and external_drive objects to create a total_storage object, and print the capacity of total_storage.