NameError:全局名称'集合'没有定义

时间:2013-04-24 04:47:59

标签: python class

作为一项任务,我正在尝试创建两个类:一个类Book,查看是否已检出书籍并在书中返回标题,作者和页码(这些是输入变量)和另一个名为Library的类将标题 - 作者对添加到字典中,并查看是否签出了某个特定的书。每次我尝试运行它时都会收到错误消息。我该如何解决这个奇怪的错误?

这是我的代码:

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages
        self.checkedOut = False

    def checked_Out(self):
        print(self.checkedOut)
        return self.checkedOut

    def change_value_of_checkedOut(self):
        if self.checkedOut == False:
            self.checkedOut = True
            print("Switched from False to True.")
        elif self.checkedOut == True:
            self.checkedOut = False
            print("Switched from True to False.")

    def return_pages(self):
        print(self.pages)
        return self.pages

    def return_title(self):
        print(self.title)
        return self.title

class Library:
    def __init__(self):
        collection = {}

    def addExistingBook(self, book):
        collection[book.title] = book.author

    def addNewBook(self, title, author, pages):
        new_book = Book(title, author, pages)
        collection[title] = new_book.author

    def change_checked_out_status(self, title):
        if title in collection.keys():
            title.change_value_of_checkedOut()
        else:
            print("This book is not in the collection.")

def main():
    title = str(input("Enter the title of the book. "))
    author = str(input("Enter the author of the book. "))
    pages = int(input("Enter the number of pages in the book. "))
    myBook = Book(title, author, pages)
    myLib = Library()
    myLib.addExistingBook(myBook)

main()

以下是我尝试运行时会发生的情况:

Enter the title of the book. The Count of Monte Cristo
Enter the author of the book. Alexandre Dumas
Enter the number of pages in the book. 1250
Traceback (most recent call last):
  File "C:/Python33/Class Programs/book_library_classes.py", line 56, in <module>
    main()
  File "C:/Python33/Class Programs/book_library_classes.py", line 54, in main
    myLib.addExistingBook(myBook)
  File "C:/Python33/Class Programs/book_library_classes.py", line 36, in addExistingBook
    collection[book.title] = book.author
NameError: global name 'collection' is not defined

6 个答案:

答案 0 :(得分:2)

您在collection中将__init__定义为本地变量:

def __init__(self):
    collection = {}

但这并不能使它成为一个实例变量。你必须明确地这样做:

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

另外,我不会制作这样的方法:

def return_title(self):
    print(self.title)
    return self.title

它们只是对直截了当的book.title属性的另一层混淆。

此外,您无需编写.keys()if key in dictionary是首选语法:

if title in self.collection:

答案 1 :(得分:1)

在引用集合的位置添加self.collection

答案 2 :(得分:1)

必须使用self.propertyName访问类成员。您的代码应如下所示:

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

     ....

答案 3 :(得分:0)

Collection课程中,您必须使用内部collection

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

最后一行看起来很可疑。你的意思是:

self.collection[book.title] = book

答案 4 :(得分:0)

集合是一个局部变量

尝试使用self.collection在函数中引用它,这可以解决您的问题。

答案 5 :(得分:0)

你应该告诉python collection属于库实例,myLib

修改图书馆类,说:self.collection您目前所拥有的任何地方collection 即。

class Library:
    def __init__(self):
        self.collection = {}

    def addExistingBook(self, book):
        self.collection[book.title] = book.author

    def addNewBook(self, title, author, pages):
        new_book = Book(title, author, pages)
        self.collection[title] = new_book.author

    def change_checked_out_status(self, title):
        if title in self.collection.keys():
            title.change_value_of_checkedOut()
        else:
            print("This book is not in the collection.")

希望这有帮助!