Python:Object()不接受任何参数错误,即使整个类模块正确缩进
我已经创建了类书,即使我正确地编写和缩进了书中的方法(实际上我100%地从教科书中复制了代码),我也无法创建类实例。这是我的班级:Python:Object()不接受任何参数错误,即使整个类模块正确缩进,python,oop,Python,Oop,我已经创建了类书,即使我正确地编写和缩进了书中的方法(实际上我100%地从教科书中复制了代码),我也无法创建类实例。这是我的班级: class Book: """ Information about a book, including title, list of authors, publisher, ISBN, and price. """ def __init__(self, title, authors, publisher, isbn, price):
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()不带参数”错误。我不知道我做错了什么;就像我说的,我从课本上复制了所有这些代码,但还是出现了一个错误。任何帮助都将不胜感激。您到底面临什么问题?我运行了这个,它执行得非常完美
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
你到底面临什么问题?我运行了这个,它执行得非常完美
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
您的
num\u authors
方法缩进不正确。帮助文本和return
语句的缩进都应该比def
缩进得更远
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
应该是
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
确保不要混合使用制表符和空格。使用一个或另一个,但不能同时使用两个,这会干扰python确定缩进级别的能力
虽然整个“标签与空间”的争论将永远持续下去,但强烈建议使用空间
空格是首选的缩进方法
制表符应仅用于与已缩进制表符的代码保持一致
Python3不允许混合使用制表符和空格进行缩进
使用制表符和空格混合缩进的Python 2代码应该转换为只使用空格
为自己准备一个代码编辑器,当你按tab键时,它可以自动插入4个空格,或者都是不错的选择。您的
num\u authors
方法缩进不正确。帮助文本和return
语句的缩进都应该比def
缩进得更远
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
应该是
def num_authors(self):
""" (Book) -> int
Return the number of authors for the book.
"""
return len(self.authors)
确保不要混合使用制表符和空格。使用一个或另一个,但不能同时使用两个,这会干扰python确定缩进级别的能力
虽然整个“标签与空间”的争论将永远持续下去,但强烈建议使用空间
空格是首选的缩进方法
制表符应仅用于与已缩进制表符的代码保持一致
Python3不允许混合使用制表符和空格进行缩进
使用制表符和空格混合缩进的Python 2代码应该转换为只使用空格
为自己准备一个代码编辑器,当你按tab键时,它可以自动插入4个空格,或者都是很好的选择。我只在声明了definit()函数之后运行了您的代码,它工作正常。如其他答案所述,检查以下各项:
我只在声明了definit()函数之后运行了您的代码,它工作正常。如其他答案所述,检查以下各项:
您返回的
num_authors
缩进错误,但我认为您的代码中的缩进是正确的,那么python的哪个版本呢?请确保您查看的是正确的文件,并且没有意外地在某处重新定义Book
。我看到一些混合制表符和空格的问题,尤其是关于num\u作者的定义。在编辑器中打开“显示空白”,查看是否有任何其他混合制表符和空格可能导致\uuuu init\uuuu
超出类定义或类似内容。在此处发布代码时是否以任何方式编辑缩进?返回的num\u authors
缩进错误,但是我假设您的代码中正确地包含了它,那么python的哪个版本呢?请确保您查看的是正确的文件,并且没有意外地在某个地方重新定义了Book
。我看到了一些混合制表符和空格的问题,特别是在num\u authors
的定义方面。在编辑器中打开“显示空白”,查看是否有任何其他混合制表符和空格可能导致\uuuu init\uuuu
超出类定义或类似内容。在此处发布代码时,您是否以任何方式编辑缩进?我总是收到以下错误:回溯(最近一次调用):文件“”,第1行,在python_book=book.book(“实用编程”、[“坎贝尔”]、“实用书架”、'00-111',25)中,TypeError:object()不带参数,实际上我不确定问题出在哪里。从各种迹象来看,它应该是有效的。我只是简单地使用类模块,在shell中键入OP中的命令,仍然会得到一个错误。您使用的是哪个Python版本?另外,若它并没有在“importbook”行抛出错误,那个么缩进和目录问题就不存在了。是不是在导入行上抛出了错误?我总是遇到这样的错误:回溯(最近一次调用最后一次):文件“”,第1行,在python_book=book.book(“实用编程”、[“坎贝尔”]、“实用书架”、'00-111',25)类型错误:object()不带参数,我实际上不确定问题出在哪里。从各种迹象来看,它应该是有效的。我只是简单地使用类模块,在shell中键入OP中的命令,仍然会得到一个错误。您使用的是哪个Python版本?另外,若它并没有在“importbook”行抛出错误,那个么缩进和目录问题就不存在了。是否在导入行中抛出错误?打开“编辑”视图显示这是一个混合制表符和空格的问题。@user2357112我已更新我的答案,以包含有关混合制表符和空格的其他信息。打开“编辑”视图显示这是一个混合制表符和空格的问题。@us