문제

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?

도움이 되었습니까?

해결책

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top