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

2024/11/20 14:37:07

The class:

class Book(object):def __init__(self, title, author):self.title = titleself.author = authordef 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.

Answer
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)
https://en.xdnf.cn/q/26311.html

Related Q&A

Retrieve name of column from its Index in Pandas

I have a pandas dataframe and a numpy array of values of that dataframe. I have the index of a specific column and I already have the row index of an important value. Now I need to get the column name …

Purpose of return self python

I have a problem with return selfclass Fib: def __init__(self, max):self.max = maxdef __iter__(self): self.a = 0self.b = 1return selfdef __next__(self):fib = self.aif fib > self.max:raise StopIterat…

tempfile.TemporaryDirectory context manager in Python 2.7

Is there a way to create a temporary directory in a context manager with Python 2.7?with tempfile.TemporaryDirectory() as temp_dir:# modify files in this dir# here the temporary diretory does not exis…

Matplotlib returning a plot object

I have a function that wraps pyplot.plt so I can quickly create graphs with oft-used defaults:def plot_signal(time, signal, title=, xlab=, ylab=,line_width=1, alpha=1, color=k,subplots=False, show_grid…

Where is the history file for ipython

I can not determine where the ipython is storing its history.a. There is no ~/.pythonhistory:12:49:00/dashboards $ll ~/.py* ls: /Users/steve/.py*: No such file or directoryb. Nothing special in the pyt…

How do I find what is using memory in a Python process in a production system?

My production system occasionally exhibits a memory leak I have not been able to reproduce in a development environment. Ive used a Python memory profiler (specifically, Heapy) with some success in th…

In Django is there a way to display choices as checkboxes?

In the admin interface and newforms there is the brilliant helper of being able to define choices. You can use code like this:APPROVAL_CHOICES = ((yes, Yes),(no, No),(cancelled, Cancelled), )client_app…

How to get the first 2 letters of a string in Python?

Lets say I have a string str1 = "TN 81 NZ 0025" two = first2(str1) print(two) # -> TNHow do I get the first two letters of this string? I need the first2 function for this.

Python sqlite3 version

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >…

Python debugger (pdb) stopped handlying up/down arrows, shows ^[[A instead

I am using python 2.6 in a virtualenv on an Ubuntu Linux 11.04 (natty) machine. I have this code in my (django) python code:import pdb ; pdb.set_trace()in order to launch the python debugger (pdb).Up u…