2
新年快樂!Python類繼承
我是Python新手,一直在嘗試使用類繼承。我創建了下面的代碼並有幾個問題 -
- 爲什麼shDate3的類型爲numpy.datetime64而不是SHDate3? shDate似乎是SHDate類型,這是我期待的行爲。
- 爲什麼不能創建shDate2?我收到「'一個整數是必需的'」錯誤...
非常感謝!
from datetime import *
from numpy import *
class SHDate(date):
def __init__(self, year, month, day):
date.__init__(self, year, month, day)
class SHDate2(date):
def __init__(self, dateString):
timeStruct = strptime(dateString, "%Y-%m-%d")
date.__init__(self, timeStruct.tm_year, timeStruct.tm_mon, timeStruct.tm_mday)
class SHDate3(datetime64):
def __init__(self, dateString):
super(SHDate3, self).__init__(dateString)
if __name__ == '__main__':
shDate = SHDate(2010,1,31)
print type(shDate)
shDate3 = SHDate3("2011-10-11")
print shDate3
print type(shDate3)
shDate2 = SHDate2("2011-10-11")
print shDate2