I need a class extending datetime.date
adding logic that is relevant to my application. It used to work well with Python 2, but I don't understand how to write the constructor in Python 3. I thought the standard way to write a derived constructor in Python 3 was like this:
from datetime import date
class my_date(date):
def __init__(self, year, month, day):
super().__init__(year, month, day)
date(2020, 4, 18)
my_date(2020, 4, 18)
However, this code fails with:
Traceback (most recent call last):
File "/tmp/test.py", line 8, in <module>
my_date(2020, 4, 18)
File "/tmp/test.py", line 5, in __init__
super().__init__(year, month, day)
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
What should I do to properly call the super constructor?
In particular, datetime.date
clearly accepts to be initialized with three numbers. Why does the error message say it takes one argument? And why is the problem in object.__init__()
instead of in datetime.date.__init__()
?