I got this from the python docuemtation.
date2 = date1 + timedelta --->date2 is timedelta.days days removed from date1. (1)
date2 = date1 - timedelta---> Computes date2 such that date2 + timedelta == date1. (2)

timedelta is an object representing a time difference and a date object represent a certain point in time. How can you just add (as in use the + operator) two objects of different types together? (it works correctly but I want to know how this is possible.)

Recommended Answers

All 2 Replies

Well, x + y is transformed into x.__add__(y) if x has a __add__ method. If it doesn't, but y has a __radd__ method, it's transformed into y.__radd__(x) . So all you need to do is to define __add__ and possibly __radd__ methods in your classes.

Thanks... good explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.