不打印对象详细信息
Not Printing object details
正在尝试通过方法 getBookDetails 打印 Book 对象的详细信息。我想要的输出是“Ready Player One”,“Cline, Ernest”,2011,但收到消息说“<main 的>
class Book():
def __init__(self, title = "", author = "", year = None):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
string = ("Title: {}, Author: {}, Year: {}"\
.format(self.title, self.author, self.year))
return string
b = Book("Ready Player One", "Cline, Ernest", 2011)
print(b.getBookDetails)
缺少括号
- 要调用函数,您必须在末尾添加
()
括号。
class Book():
def __init__(self, title = "", author = "", year = None):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
string = ("Title: {}, Author: {}, Year: {}"\
.format(self.title, self.author, self.year))
return string
b = Book("Ready Player One", "Cline, Ernest", 2011)
print(b.getBookDetails())
输出:-
>>> print(b.getBookDetails)
<bound method Book.getBookDetails of <__main__.Book object at 0x0000015E9B6F8130>>
>>> print(b.getBookDetails())
Title: Ready Player One, Author: Cline, Ernest, Year: 2011
正在尝试通过方法 getBookDetails 打印 Book 对象的详细信息。我想要的输出是“Ready Player One”,“Cline, Ernest”,2011,但收到消息说“<main 的
class Book():
def __init__(self, title = "", author = "", year = None):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
string = ("Title: {}, Author: {}, Year: {}"\
.format(self.title, self.author, self.year))
return string
b = Book("Ready Player One", "Cline, Ernest", 2011)
print(b.getBookDetails)
缺少括号
- 要调用函数,您必须在末尾添加
()
括号。
class Book():
def __init__(self, title = "", author = "", year = None):
self.title = title
self.author = author
self.year = year
def getTitle(self):
return self.title
def getAuthor(self):
return self.author
def getYear(self):
return self.year
def getBookDetails(self):
string = ("Title: {}, Author: {}, Year: {}"\
.format(self.title, self.author, self.year))
return string
b = Book("Ready Player One", "Cline, Ernest", 2011)
print(b.getBookDetails())
输出:-
>>> print(b.getBookDetails)
<bound method Book.getBookDetails of <__main__.Book object at 0x0000015E9B6F8130>>
>>> print(b.getBookDetails())
Title: Ready Player One, Author: Cline, Ernest, Year: 2011