What are some real life cases which could be implemented using special methods?
Think of any complex problems that we solve every day like:
- adding time intervals, like: add 21 hours 58 minutes 50 seconds to 1hr 45 minutes 22 seconds;
- adding length measurements expressed in the imperial measurement system, like: add 2 feet 8 inches to 1 yard 1 foot 4 inches.
With classes equipped with custom special methods, the tasks may start to look simpler.
Of course, don’t forget to check the type of the objects passed as arguments to special methods. If someone uses your classes, they might use them incorrectly, so a good __add__ method should check whether it’s possible to perform the addition before doing it, or else raise an exception.