Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - ValueError: cannot switch from manual field specification to automatic field numbering

The 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)

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'

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'

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

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

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...