Question

I've the following code:

from datetime import datetime, timedelta


class MyDate(datetime):
      pass

d = MyDate.now()
t = timedelta(1, 1, 1)
print type(d)
print type(d - t)

The output is the following:

<class '__main__.MyDate'>
<type 'datetime.datetime'> 

So now to my question, why does a subclass minus timedelta result in the super class?

In addition is there a workaround for this where I don't have to encapsulate a datetime object and redirect all methods of datetime?

Was it helpful?

Solution

Your MyDate sub-class doesn't override the subtraction function __sub__(), so inherits the function from the base class, which returns an instance of the base class.

A workaround for what? What are you trying to achieve by sub-classing datetime?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top