【问题标题】:Saving objects to a list python; List not updating for some reason?将对象保存到列表 python;列表由于某种原因没有更新?
【发布时间】:2014-10-07 00:40:10
【问题描述】:

好吧,我要做的是在用户创建 NoteSet 时让对象以列表形式保存。当我输入 NoteSet('ex','example',True) 时,它会正确地将对象附加到列表 db 中。我创建了一个名为 makeNewNoteSet() 的函数,它似乎工作正常,但它没有附加到数据库列表中。我不知道为什么。

import sys
import datetime
import pickle
notesets = []
db = []
def save():
    global db
    filename = "notesets.dat"
    file = open(filename, "wb")
    if file == None:
        print("There was an error creating your file")
        return
    pickle.dump(db, file)
    file.close()
    print("Saved words to ",filename)
def load():
    global db
    filename = "notesets.dat"
    file = open(filename, "rb")
    db = pickle.load(file)
    print("There are ",len(db)," Note Sets")
    file.close()

class NoteSet:    
    nextseqNum = len(db)+2
    def __init__(self,name,description,hidden):
        global db
        self.seqNum = NoteSet.nextseqNum
        self.name        = name
        self.description = description
        self.dateCreated = datetime.date.today()
        self.hidden      = hidden
        self.notes       = list()
        NoteSet.nextseqNum += 1
        print(self)
        notesets.append(self)
        notelist = [self.seqNum,self.name,self.description,self.dateCreated,self.hidden,self.notes]
        print(notelist)
        db.append(notelist)

        NoteSet.nextseqNum  += 1
    def __str__(self):
            printstr = str(self.seqNum),self.name,self.description,str(self.dateCreated)
            printstr = str(printstr)
            return printstr

class Note:
    nextseqNum = 0
    def __init__(self,text,dateCreated,description,category,priority,hidden):
        self.text        = text
        self.dateCreated = str
        self.dateRead    = str
        self.description = str
        self.category    = str
        self.priority    = int
        self.hidden      = bool
        self.seqNum      = Note.nextseqNum
        Note.nextseqNum  += 1

def main():
    while True:
        load()
        printMainMenu()
        selection = int(input("? "))

        if selection == 1:
            listNoteSets()
        elif selection == 2:
            listAllNoteSets()
        elif selection == 3:
            makeNewNoteSet()
        elif selection == 4:
            selectNoteSet()    # this makes the working note set
        elif selection == 5:
            deleteNoteSet()
        elif selection == 6:
            sys.exit()
        else:
            print("Invalid choice")

def printMainMenu():
    print("1.  List note sets")
    print("2.  List all note sets (including hidden sets)")
    print("3.  Make a new note set")
    print("4.  Select a working note set")
    print("5.  Delete a note set")
    print("6.  Quit")

def listNoteSets():
    num = 0
    for row in db:
        if db[num][4] == False:
            print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
        input("[Press return to see main menu]")
        main()
def listAllNoteSets():
    num = 0
    for row in db:
        print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
        input("[Press return to see main menu]")
        main()
def makeNewNoteSet():
    num = 0
    name = input("What would you like to name your note set? ---  ")
    for row in db:
        if name == db[num][1]:
            print("That note set has already been created")
            makeNewNoteSet()                
    description = input("Describe your note set briefly ---  ")
    hidden      = input("Would you like this note set to be hidden? ---  ")
    if hidden == 'y' or 'yes':
        hidden = True
    else:
        hidden = False
    NoteSet(name, description, hidden)
    print("noteset created you can now access it through the menu")
    input("[Press enter to return to menu]")
    main()
def selectNoteSet():
    num = 0
    for row in db:
        print('#',db[num][0],'   ',db[num][1],'----',db[num][2])
        num += 1
    response = input("Enter the number assosciated with the noteset you would like to access")
    print("Note set #",response," was selected")


main()

【问题讨论】:

  • 我在 MakeNewNoteSet 中没有看到任何添加到 db 的代码。
  • @alexis:很难找到,在NoteSet.__init__ 方法中。我仍在试图找出可能出了什么问题。这段代码肯定不会通过代码审查!
  • NoteSet 类中的 db.append(notelist)

标签: python list class object


【解决方案1】:

在makeNewNoteSet() 中添加新注释后,您调用main(),它调用load(),它会覆盖您刚刚更改的数据库的内存副本。你可能想在那儿的某个地方打电话给save()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2018-01-28
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    相关资源
    最近更新 更多