我不知道为什么通过创建两个不同的实例我得到相同的结果?我的逻辑有什么问题? (更新工作)两个不同的实例给出相同的结果(Python)
基本上,这个类应该在根据一年
代码本身创建日期列表:
class Date:
def __init__(self, year):
self.year = year
def dateStr(self, date):
if date < 10:
date = '0' + str(date)
else:
date = str(date)
return date
def daysInMonth(self, month):
if month == 4 or month == 6 or month == 9 or month == 11:
endDate = 30
if month == 1 or month == 3 or month ==5 or month == 7 or month ==8 or month == 10 or month == 12:
endDate = 31
if self.year%4 == 0 and month == 2:
endDate = 29
if self.year%4 != 0 and month == 2:
endDate = 28
return endDate
def makeDate(self):
self.date = []
month = 1
while month <= 12:
day = 1
while day <= self.daysInMonth(month):
self.date.append(str(self.dateStr(day)) + u'.' + str(self.dateStr(month)) + u'.' + str(self.year))
day += 1
month += 1
return self.date
def __str__(self):
return str(self.makeDate())
def __len__(self):
return len(self.makeDate())
def __getitem__(self, key):
return self.makeDate()[key]
date1 = Date(2012)
date2 = Date(2013)
print date1[364]
print date2[364]
感谢支持,
亚历
你会得到什么结果? –
'len(date)<366)'看起来不正确。您可能必须在'makeDate()''date''中使用'全球日期' – karthikr
2012年的两个日期列表,而我预计2012年会有一个日期,2013年会有另一个。 – Alekz112