2012-01-01 2 views
2

새해 복 많이 받으세요!Python 클래스 상속

저는 파이썬을 처음 사용하고 있으며, 클래스 상속을 실험 해 왔습니다. 아래 코드를 작성하고 몇 가지 질문이 있습니다.

  1. shDate3 대신 numpy.datetime64 유형의 shDate3이있는 이유는 무엇입니까? shDate는 SHDate 유형 인 것 같습니다. 이는 예상했던 동작입니다.
  2. 왜 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 

답변

2

빠른 답변 :

  1. 당신이 중 하나를 type 또는 isinstance, 그들은 다른 사용해야 할 때 알고 있어야합니다. 이 question을 살펴보고 싶다면 typeisinstance 사용법을 명확히 알 수 있습니다.
  2. 클래스이므로 date 클래스를 사용자 지정하려면 __init__을 사용하면 안됩니다. 이 question은 해당 클래스의 인스턴스를 사용자 정의하는 방법에 대해 설명합니다.