我已经创建了Book类,即使我正确地编写和缩进了Book方法(实际上我100%地复制了教科书中的代码),我仍然无法创建类实例。下面是我的类:
class Book:
""" Information about a book, including title, list of authors,
publisher, ISBN, and price.
"""
def __init__(self, title, authors, publisher, isbn, price):
""" (Book, str, list of str, str, str, number) -> Nonetype
Create a book, with a title, authors, publisher, isbn number, and price.
>>> python_book = Book( \
"Practical Programming", \
["Campbell", "Gries", "Montojo"], \
"Pragmatic Bookshelf", \
'000-1111', \
25.0)
>>> python_book.title
"Practical Programming"
>>> python_book.authors
["Campbell", "Gries", "Montojo"]
>>> python_book.publisher
"Pragmatic Bookshelf"
>>> python_book.ISBN
'000-1111'
>>> python_book.price
25.0
"""
self.title = title
self.authors = authors[:]
self.publisher = publisher
self.ISBN = isbn
self.price = price
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
def __str__(self):
""" (Book) -> str
Return a human-readable string representation of this Book.
"""
return "Title: {0}, Authors: {1}, Publisher: {2}, ISBN: {3}, Price: {4}".format(self.title, self.authors, self.publisher, self.ISBN, self.price)
def __eq__(self, other):
""" (Book, Book) -> bool
Return True iff this book and other have the same ISBN.
"""
return self.ISBN == other.ISBN下面是我在shell中的代码:
>>> import book
>>> python_book = book.Book("Practical Programming", ["Campbell", "Gries", "Montojo"], "Pragmatic Bookshelf", '00-111', 25)即使这样,我还是得到了"object()不带参数“的错误。我不知道我做错了什么;就像我说的,我从课本上抄袭了所有这些代码,但仍然得到了一个错误。任何帮助都将不胜感激。
发布于 2017-05-06 12:38:20
你面临的问题到底是什么?我运行了这个程序,它执行得很完美。
class Book:
""" Information about a book, including title, list of authors,
publisher, ISBN, and price.
"""
def __init__(self, title, authors, publisher, isbn, price):
""" (Book, str, list of str, str, str, number) -> Nonetype
Create a book, with a title, authors, publisher, isbn number, and price.
>>> python_book = Book( \
"Practical Programming", \
["Campbell", "Gries", "Montojo"], \
"Pragmatic Bookshelf", \
'000-1111', \
25.0)
>>> python_book.title
"Practical Programming"
>>> python_book.authors
["Campbell", "Gries", "Montojo"]
>>> python_book.publisher
"Pragmatic Bookshelf"
>>> python_book.ISBN
'000-1111'
>>> python_book.price
25.0
"""
self.title = title
self.authors = authors[:]
self.publisher = publisher
self.ISBN = isbn
self.price = price
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
def __str__(self):
""" (Book) -> str
Return a human-readable string representation of this Book.
"""
return "Title: {0}, Authors: {1}, Publisher: {2}, ISBN: {3}, Price: {4}".format(self.title, self.authors, self.publisher, self.ISBN, self.price)
def __eq__(self, other):
""" (Book, Book) -> bool
Return True iff this book and other have the same ISBN.
"""
return self.ISBN == other.ISBN
python_book = Book("Practical Programming", ["Campbell", "Gries", "Montojo"], "Pragmatic Bookshelf", '00-111', 25)
print (python_book)输出:
Title: Practical Programming, Authors: ['Campbell', 'Gries', 'Montojo'], Publisher: Pragmatic Bookshelf, ISBN: 00-111, Price: 25发布于 2017-05-06 12:48:57
在只声明了def init()函数之后,我运行了您的代码,它工作得很好。如其他答案中所述,请检查以下各项:
num_authors()
https://stackoverflow.com/questions/43816731
复制相似问题