Reputation: 3566
For whatever reason, I want to subclass datetime.time so that the subclass can be initialized by another datetime.time object. This sadly doesn't work:
class MyTime(datetime.time):
def __init__(self, t):
super().__init__(t.hour, t.minute, t.second, t.microsecond)
>>> t=datetime.time(10,20,30,400000)
>>> MyTime(t)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
obviously I'm doing something stupid, but what is it?
Upvotes: 2
Views: 508
Reputation: 91017
super()
doesn't work as is in Python 2.x. You want to use super(MyTime, self)
instead.
You'll have to override __new__
instead of __init__
in this case:
class MyTime(datetime.time):
def __new__(cls, t):
return datetime.time.__new__(cls, t.hour, t.minute, t.second, t.microsecond)
print MyTime(datetime.time(10,20,30,400000))
prints
10:20:30.400000
Upvotes: 3