Python - 返回对象而不是字符串
Python - returning something as an object instead of string
我必须创建一个日期 class,我们必须实现的两个方法是 nextday() 和 prevday()。这是我的代码:
class Date:
"""
A class for establishing a date.
"""
min_year = 1800
def __init__(self, month = 1, day = 1, year = min_year):
"""
Checks to see if the date is real.
"""
self.themonth = month
self.theday = day
self.theyear = year
def nextday(self):
"""
Returns the date of the day after given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
maxdays = monthdays[self.themonth]
if self.theday != maxdays:
return '{0}/{1}/{2}'.format(self.themonth, self.theday+1, self.theyear)
elif self.theday == maxdays and self.themonth == 12:
return '{0}/{1}/{2}'.format(1,1,self.theyear+1)
elif self.theday == maxdays and self.themonth != 12:
return '{0}/{1}/{2}'.format(self.themonth+1, 1, self.theyear)
def prevday(self):
"""
Returns the date of the day before given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if self.theday == 1 and self.themonth == 1:
return Date(12, monthdays[11], self.theyear-1)
elif self.theday == 1 and self.themonth != 1:
return Date(self.themonth -1, monthdays[self.themonth-1], self.theyear)
elif self.theday != 1:
return Date(self.themonth, self.theday - 1, self.theyear)
如您所见,nextday return 是第二天,但它是一个字符串对象。然而,前一天只是 returns 看起来像这样的东西:<main.Date object at 0x039E3270>
如何使这些函数 return 成为另一个日期对象?
prevday 正在返回一个新的 Date 对象,尽管它看起来很奇怪,因为您还没有实现 __repr__()
。只需更改 nextday 以使用相同的 Date(...)
语法。
好的,试试,
class Date:
"""
A class for establishing a date.
"""
min_year = 1800
def __init__(self, month = 1, day = 1, year = min_year):
"""
Checks to see if the date is real.
"""
self.themonth = month
self.theday = day
self.theyear = year
def nextday(self):
"""
Returns the date of the day after given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
maxdays = monthdays[self.themonth]
if self.theday != maxdays:
return Date(self.themonth, self.theday+1, self.theyear)
elif self.theday == maxdays and self.themonth == 12:
return Date(1,1,self.theyear+1)
elif self.theday == maxdays and self.themonth != 12:
return Date(self.themonth+1, 1, self.theyear)
def prevday(self):
"""
Returns the date of the day before given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if self.theday == 1 and self.themonth == 1:
return Date(12, monthdays[11], self.theyear-1)
elif self.theday == 1 and self.themonth != 1:
return Date(self.themonth -1, monthdays[self.themonth-1], self.theyear)
elif self.theday != 1:
return Date(self.themonth, self.theday - 1, self.theyear)
def year_is_leap(self):
return True
def __repr__(self):
return 'Date(%s,%s,%s)' % (self.themonth,self.theday,self.theyear)
当然你必须执行year_is_leap()。
>>> Date(1,1,1990).nextday()
Date(1,2,1990)
>>> Date(1,1,1990).prevday()
Date(12,31,1989)
我必须创建一个日期 class,我们必须实现的两个方法是 nextday() 和 prevday()。这是我的代码:
class Date:
"""
A class for establishing a date.
"""
min_year = 1800
def __init__(self, month = 1, day = 1, year = min_year):
"""
Checks to see if the date is real.
"""
self.themonth = month
self.theday = day
self.theyear = year
def nextday(self):
"""
Returns the date of the day after given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
maxdays = monthdays[self.themonth]
if self.theday != maxdays:
return '{0}/{1}/{2}'.format(self.themonth, self.theday+1, self.theyear)
elif self.theday == maxdays and self.themonth == 12:
return '{0}/{1}/{2}'.format(1,1,self.theyear+1)
elif self.theday == maxdays and self.themonth != 12:
return '{0}/{1}/{2}'.format(self.themonth+1, 1, self.theyear)
def prevday(self):
"""
Returns the date of the day before given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if self.theday == 1 and self.themonth == 1:
return Date(12, monthdays[11], self.theyear-1)
elif self.theday == 1 and self.themonth != 1:
return Date(self.themonth -1, monthdays[self.themonth-1], self.theyear)
elif self.theday != 1:
return Date(self.themonth, self.theday - 1, self.theyear)
如您所见,nextday return 是第二天,但它是一个字符串对象。然而,前一天只是 returns 看起来像这样的东西:<main.Date object at 0x039E3270> 如何使这些函数 return 成为另一个日期对象?
prevday 正在返回一个新的 Date 对象,尽管它看起来很奇怪,因为您还没有实现 __repr__()
。只需更改 nextday 以使用相同的 Date(...)
语法。
好的,试试,
class Date:
"""
A class for establishing a date.
"""
min_year = 1800
def __init__(self, month = 1, day = 1, year = min_year):
"""
Checks to see if the date is real.
"""
self.themonth = month
self.theday = day
self.theyear = year
def nextday(self):
"""
Returns the date of the day after given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
maxdays = monthdays[self.themonth]
if self.theday != maxdays:
return Date(self.themonth, self.theday+1, self.theyear)
elif self.theday == maxdays and self.themonth == 12:
return Date(1,1,self.theyear+1)
elif self.theday == maxdays and self.themonth != 12:
return Date(self.themonth+1, 1, self.theyear)
def prevday(self):
"""
Returns the date of the day before given date.
"""
m = Date(self.themonth, self.theday, self.theyear)
monthdays = [31, 29 if m.year_is_leap() else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if self.theday == 1 and self.themonth == 1:
return Date(12, monthdays[11], self.theyear-1)
elif self.theday == 1 and self.themonth != 1:
return Date(self.themonth -1, monthdays[self.themonth-1], self.theyear)
elif self.theday != 1:
return Date(self.themonth, self.theday - 1, self.theyear)
def year_is_leap(self):
return True
def __repr__(self):
return 'Date(%s,%s,%s)' % (self.themonth,self.theday,self.theyear)
当然你必须执行year_is_leap()。
>>> Date(1,1,1990).nextday()
Date(1,2,1990)
>>> Date(1,1,1990).prevday()
Date(12,31,1989)