Reputation: 486
Happy new years guys!
I'm new to Python and have been experimenting with class inheritance. I created the code below and have a few questions -
Thanks a lot!
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
Upvotes: 2
Views: 714
Reputation: 1285
Quick answers:
type
or isinstance
, they are different. You may want to take a look at this question, it clarifies type
and isinstance
usage.__init__
to custom your date
class, because it is an immutable class. This question provides some discussion on customizing instances for those classes.Upvotes: 2