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__()?
Answer from the comments:
Jens
Looking at the Python source of datetime.date you’ll notice that it doesn’t have an
__init__()method, only a__new__()constructor. That would explain the above error. Also note thatyear,month,dayare decorated (i.e. read only) properties, not instance values.
accdias
Replace
super().__init__(year, month, day)withpass
from datetime import date
class my_date(date):
pass
d1 = date(2020, 4, 18)
d2 = my_date(2020, 4, 18)
print(d1,d2)
Outputs:
2020-04-18 2020-04-18
You might think about using composition instead of inheritance for whatever you need done:
class MyDateClass:
def __init__(self, a_date):
self.my_date = a_date
# add members/methods that do stuff wiht a_date
instead of inheriting date.
See
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With