Alg*_*bra 16 python string-formatting python-3.x
班级:
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)
Run Code Online (Sandbox Code Playgroud)
从中创建我的书的实例:
In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'
Run Code Online (Sandbox Code Playgroud)
请注意,我没有初始化属性'self.press',而是在get_entry方法中使用它.继续输入数据.
mybook.press = 'Murach'
mybook.price = 'download'
Run Code Online (Sandbox Code Playgroud)
直到现在,我可以指定所有输入的数据 vars
In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}
Run Code Online (Sandbox Code Playgroud)
我在控制台中输入了很多关于mybook的数据.当尝试调用get_entry方法时,错误报告.
mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.
Run Code Online (Sandbox Code Playgroud)
所有这一切都在控制台上以交互模式进行.我珍惜数据输入,进一步mybook在文件中挑选对象.但是,它有缺陷.如何在交互模式下拯救它.或者我必须重新开始.
Jea*_*bre 24
return "{0} by {1} on {}".format(self.title, self.author, self.press)
Run Code Online (Sandbox Code Playgroud)
这不起作用.如果您指定头寸,则必须在结束时执行:
return "{0} by {1} on {2}".format(self.title, self.author, self.press)
Run Code Online (Sandbox Code Playgroud)
在您的情况下,最好是自动离开python处理:
return "{} by {} on {}".format(self.title, self.author, self.press)
Run Code Online (Sandbox Code Playgroud)