我有這段代碼創建一個新的note..WHen我嘗試打印我碰到下面的錯誤,即使它打印輸出Python:「TypeError:__str__返回非字符串」,但仍然打印輸出?
Error:
C:\Python27\Basics\OOP\formytesting>python notebook.py
Memo=This is my first memo, Tag=example
Traceback (most recent call last):
File "notebook.py", line 14, in <module>
print(firstnote)
TypeError: __str__ returned non-string (type NoneType)
note.py
import datetime
class Note:
def __init__(self, memo, tags):
self.memo = memo
self.tags = tags
self.creation_date = datetime.date.today()
def __str__(self):
print('Memo={0}, Tag={1}').format(self.memo, self.tags)
if __name__ == "__main__":
firstnote = Note('This is my first memo','example')
print(firstnote)
這就是我需要的!如何保存這個答案以供將來參考...>? – flowera 2018-01-04 18:30:11