OK所以我创建了两个名为Note和Notebook的类。AttributeError:元组对象没有属性read_note()
class Note:
""" A Note """
note_number = 1
def __init__(self, memo="", id=""):
""" initial attributes of the note"""
self.memo = memo
self.id = Note.note_number
Note.note_number += 1
def read_note(self):
print(self.memo)
class NoteBook:
"""The Notebook"""
def __init__(self):
self.note_book = []
def add_notes(self, *args):
for note in enumerate(args):
self.note_book.append(note)
def show_notes(self):
for note in self.note_book:
note.read_note()
n1 = Note("First note")
n2 = Note("Second note")
n3 = Note("Third note")
notebook1 = NoteBook()
notebook1.add_notes(n1, n2, n3)
notebook1.show_notes()
Traceback (most recent call last):
File "C:/Users/Alan/Python3/Random stuff/notebook revisions.py", line 47, in <module>
notebook1.show_notes()
File "C:/Users/Alan/Python3/Random stuff/notebook revisions.py", line 38, in show_notes
note.read_note()
AttributeError: 'tuple' object has no attribute 'read_note'
我怎么得到属性错误?我想让我的show_notes()方法读取notebook1列表中的所有注释。
另外,如果我打印下面的语句我的结果是晦涩的消息:
print(notebook1.note_book[0])
(0, <__main__.Note object at 0x00863F30>)
我将如何解决这个问题不会产生怪异神秘的消息,并打印字符串“首先说明”,“第二注意“和”第三注“。第一季度销售价格指数:
阅读并应用https://stackoverflow.com/help/mcve。删除绒毛(.str方法)并包含基本的.add_note方法。然后包含错误消息。 – 2015-02-24 00:18:46
感谢特里我编辑了这篇文章,希望现在更清楚。 – firebird92 2015-02-24 15:42:09
复制,粘贴和运行后,我得到相同的错误。现在我可以回答问题。 – 2015-02-24 23:12:30