Python - 将某些东西作为对象而不是字符串返回

时间:2015-04-15 18:09:37

标签: python class methods

我必须创建一个Date类,我们必须实现的两个方法是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)

如您所见,次日正确地返回第二天,但它是一个字符串对象。然而,Prevday只会返回如下所示的内容:< main .Date对象位于0x039E3270> 如何让这些函数返回另一个日期对象?

2 个答案:

答案 0 :(得分:0)

prevday正在返回一个新的Date对象,虽然它看起来很奇怪,因为你还没有实现__repr__()。只需在第二天更改即可使用相同的Date(...)语法。

答案 1 :(得分:0)

好的,试试,

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)