爲什麼以下工作(Python 2.5.2)?爲什麼我不能繼承datetime.date?
>>> import datetime
>>> class D(datetime.date):
def __init__(self, year):
datetime.date.__init__(self, year, 1, 1)
>>> D(2008)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function takes exactly 3 arguments (1 given)
我想創建一個類,簡直就像datetime.date
,但有不同的__init__
功能。顯然我的功能永遠不會被調用。相反,原來的datetime.date.__init__
被調用並失敗,因爲它需要3個參數,而我正在傳入一個參數。
這是怎麼回事?這是一個線索?
>>> datetime.date.__init__
<slot wrapper '__init__' of 'object' objects>
謝謝!
另請參閱http://stackoverflow.com/questions/309 129 /爲什麼我不能繼承dict和異常在python – hop 2008-12-29 23:16:29