0

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.)

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by mahela007
1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.