gpt4 book ai didi

python - 值错误 : cannot switch from manual field specification to automatic field numbering

转载 作者:太空狗 更新时间:2023-10-29 18:00:42 27 4
gpt4 key购买 nike

类(class):

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'

请注意,我没有初始化属性“self.press”,而是在 get_entry 方法中使用它。继续输入数据。

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.

所有这些都在控制台上以交互模式进行。我珍惜输入的数据,进一步 pickle mybook 文件中的对象。然而,它是有缺陷的。如何在交互模式下拯救它。否则我必须重新开始。

最佳答案

return "{0} by {1} on {}".format(self.title, self.author, self.press)

那是行不通的。如果指定位置,则必须执行到最后:

return "{0} by {1} on {2}".format(self.title, self.author, self.press)

在你的情况下,最好让 python 自动处理:

return "{} by {} on {}".format(self.title, self.author, self.press)

关于python - 值错误 : cannot switch from manual field specification to automatic field numbering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46768088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com