首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python: Object()没有参数错误,即使整个类模块缩进正确

Python: Object()没有参数错误,即使整个类模块缩进正确
EN

Stack Overflow用户
提问于 2017-05-06 12:31:39
回答 2查看 300关注 0票数 0

我已经创建了Book类,即使我正确地编写和缩进了Book方法(实际上我100%地复制了教科书中的代码),我仍然无法创建类实例。下面是我的类:

代码语言:javascript
复制
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中的代码:

代码语言:javascript
复制
>>> import book
>>> python_book = book.Book("Practical Programming", ["Campbell", "Gries", "Montojo"], "Pragmatic Bookshelf", '00-111', 25)

即使这样,我还是得到了"object()不带参数“的错误。我不知道我做错了什么;就像我说的,我从课本上抄袭了所有这些代码,但仍然得到了一个错误。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2017-05-06 12:38:20

你面临的问题到底是什么?我运行了这个程序,它执行得很完美。

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
Title: Practical Programming, Authors: ['Campbell', 'Gries', 'Montojo'], Publisher: Pragmatic Bookshelf, ISBN: 00-111, Price: 25
票数 0
EN

Stack Overflow用户

发布于 2017-05-06 12:48:57

在只声明了def init()函数之后,我运行了您的代码,它工作得很好。如其他答案中所述,请检查以下各项:

num_authors()

  1. 您要导入的'book‘的缩进位于同一目录中。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43816731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档