我有一個友好的一個月I類享受返回一個醜陋的機器人友好的字符串:對象的字符串表示殼
In [3]: d = date(2010, 1, 31)
In [4]: m = Month(d)
In [5]: m
Out[5]: <dfa.date_range.Month at 0x7fb4d5793cc0>
我想m
顯示類似1-31-2010
。我嘗試使用unicode
和str
,就像在Django,沒有骰子:
class Month(object):
def __init__(self, dateobj):
self.dateobj = dateobj
# def __unicode__(self):
# return self.dateobj
def __str__(self):
return self.dateobj
@property
def first_day(self):
return self.dateobj.replace(day = 1)
@property
def last_day(self):
_, days_in_month = monthrange(self.dateobj.year, self.dateobj.month)
return self.dateobj.replace(day = days_in_month)
def date_range(self):
return self.first_day, self.last_day
對於d
對象,它沒有實現統一,但是有串。 str
和ipython返回不匹配。我會爲此打開一個單獨的問題。我怎樣才能讓我的python類顯示對用戶有用的東西? Terima kasih
ty輸出日期對象的詳細信息,那麼現在__str__的用途是什麼呢? – codyc4321