Python OOP Wrap-Up Problems — Put What You Learned into Code
Four end-of-chapter Python OOP problems: special methods on Point, super() in SavingsAccount, @property validation in Celsius, and area() polymorphism across shapes.
Problem 1: Design a Point class with special methods
Build a Point class that represents a 2D coordinate. You'll initialize attributes in __init__, define what + does between two points with __add__, and shape what print() shows with __str__ — three special methods working together.
Problem 2: Extend a bank account with inheritance and super()
Inherit from a parent class BankAccount to build a SavingsAccount that can apply interest. The pattern is classic inheritance: call the parent's initializer with super().__init__(...), then add the child-specific attributes on top.
Problem 3: Validate temperature with @property
Write a Celsius class that rejects assignments below absolute zero (-273.15). You'll use @property and a @value.setter to slot validation right into attribute assignment — that's the classic encapsulation pattern.
t.value = X looks like a plain assignment, but it routes through the setter, which validates the value first. Out of range → ValueError, in range → store in _value.Problem 4: Use polymorphism to compute areas in one loop
Give two different classes a method with the same name (area()), put their instances in a list, and process them with one for loop. The caller doesn't care which class is which — that's polymorphism in a nutshell.
shape.area() on each item in the list, the method matching the item's actual type is automatically picked.Nice work getting through this
That wraps up Python OOP. You've covered class and instance design, special methods like __init__ / __add__ / __str__, inheritance with super(), encapsulation with @property, polymorphism, the with statement, and type hints — pretty much every tool you need to bundle data and behavior into a single type. Now you can define your own types, attach methods to them, and treat several types through a shared interface.
The next chapter, Python Advanced, is about going beyond a single file: organizing modules and packages, and using the standard library — datetime, os, re, json, collections, dataclasses, asyncio, and more — to build the kind of processing you'll actually run into in production code.