ValueError:无法从手动字段规范切换到自动字段编号 [英] ValueError: cannot switch from manual field specification to automatic field numbering

查看:174
本文介绍了ValueError:无法从手动字段规范切换到自动字段编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

班级:

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)

从它创建我的书的一个实例:

Create an instance of my book from it:

In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'

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

Please notice that I didn't initialize attribute 'self.press',while use it in the get_entry method.Go ahead to type in data.

mybook.press = 'Murach'
mybook.price = 'download'

到目前为止,我可以使用 vars

Till now, I can specify all the data input with vars

In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}

我在控制台硬输入了很多关于 mybook 的数据.尝试调用 get_entry 方法时,错误报告.

I hardtype lot of data about mybook in the console.When try to call get_entry method, errors report.

mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.

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

All this going in interactive mode on console.I cherish the data inputed, further to pickle mybook object in file. However, it is flawed. How can rescue it in the interactive mode. or I have to restart all over again.

推荐答案

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

那行不通.如果你指定职位,你必须坚持到底:

that doesn't work. If you specify positions, you have to do it through the end:

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

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

In your case, best is to leave python treat that automatically:

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

这篇关于ValueError:无法从手动字段规范切换到自动字段编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆