1
爲什麼不是第二次調用構造函數?爲什麼不是第二次調用構造函數?
from datetime import datetime
class Time(datetime):
def __init__(self, *args):
print 5, args
try:
d = args[0]
print 8, d
datetime.__init__(self,
d.year, d.month, t.day, t.hour, t.minute, t.second)
except Exception:
print 12, args
datetime.__init__(self, args)
if __name__ == '__main__':
t = Time(1965, 1, 10)
print 17, t
u = Time(t)
print 19, u
使用Python 2.7.2,這裏的輸出:
bash-4.1$ python tmp.py
5 (1965, 1, 10)
8 1965
12 (1965, 1, 10)
17 1965-01-10 00:00:00
Traceback (most recent call last):
File "tmp.py", line 18, in <module>
u = Time(t)
TypeError: an integer is required
我期望看到:
5 Time(1965, 1, 10)
被稱爲什麼功能,如果沒有構造?