这个类:
class Book(object):
def __init__(self, title, author):
self.title = title
self.author = author
def get_entry(self):
return "{0} by {1} on {}".format(self.title, self.author, self.press)
从中创建一个我的书的实例:
In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'
请注意,我在get_entry方法中使用了属性'self.press',但我没有对其进行初始化。请继续输入数据。mybook.press = 'Murach'
mybook.price = 'download'
到目前为止,我可以使用vars
指定所有数据输入。
In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}
我在控制台中输入了很多关于我的书籍的数据。当尝试调用get_entry方法时,出现了错误报告。
mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.
在控制台上以交互模式进行所有操作。我珍视输入的数据,进一步将mybook
对象保存到文件中。然而,它有缺陷。如何在交互模式下挽救它,否则我必须重新开始。
b
吗? - Jean-François Fabre