【问题标题】:Python: object.__new__() takes no parameters [duplicate]Python:object.__new__() 不带参数[重复]
【发布时间】:2018-08-30 17:12:48
【问题描述】:

现在我正在开发一个程序,该程序允许人们进行测试,将它们保存到数据库中,然后打印出来。我不断收到错误:

Traceback (most recent call last):
File "C:/Users/Shepard/Desktop/Gradebook.py", line 50, in <module>
qs = QuestionStorage("questions.db")
TypeError: object.__new__() takes no parameters

有人知道吗?我假设它在 QuestionStorage 类的某个地方,但我不太能弄清楚任何事情。这是我第一次使用 SQLite3,我遇到了很多麻烦,如果有人可以帮助我,那就太好了。 :)

import sqlite3
class QuestionStorage(object):
    def _init_(self, path):
        self.connection = sqlite3.connect(path)
        self.cursor = self.connection.cursor()

    def Close(self):
        self.cursor.close()
        self.connection.close()

    def CreateDb(self):
        query = """CREATE TABLE questions
                 (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""
        self.cursor.exeute(query)
        self.connection.commit()
        #self.cursor.close()

    def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4):
        self.cursor.execute("""INSERT INTO questions
                                VALUES (?, ?, ?, ?, ?, ?)""", (None, Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer))
        self.connection.commit()

    def GetQuestion(self, index = None):
        self.cursor.execute("""SELECT * FROM questions WEHRE id=?""", (index,))
        res = self.cursor.fetchone()
        return res




print ("TestMaker v.1")
print ("To create a multiple choice test, follow the directions.")
testName = input ("Give your test a name.")
testQ = int(input ("How many questions will be on this test? (Numeric value only.)"))

counter = 1
while counter <= testQ:
    Answer = []
    Answer = [1,2,3,4,5,6]
    Question = input ("What is your question?")
    Answer[1] = input ("What is the first answer?")
    Answer[2] = input ("What is the second answer?")
    Answer[3] = input ("What is the third answer?")
    Answer[4] = input ("What is your last answer?")
    correctAnswer = int(input("Which answer is the correct answer? (1, 2, 3, or 4?)"))
    Answer[5] = Answer[correctAnswer]
    print (Answer[1:6])
    counter +=1
    if __name__ == "__main__":
        qs = QuestionStorage("questions.db")
        qs.CreateDb()    
        qs.AddQuestion(Question, Answer[1] , Answer[2], Answer[3], Answer[4], Answer[5])

qs.Close()

【问题讨论】:

  • 这几乎肯定与 sqlite3 无关。如果你取出所有的 sqlite3 调用,它仍然会发生吗?
  • 另外,即使您解决了这个问题,您的代码仍然无法正常工作。例如,您创建数据库testQ 次,但只创建if __name__ == "__main__",然后在最后无条件关闭它一次。另外,你为什么设置Answer = []只是为了在下一行替换它,只是为了在接下来的6行中再次替换它?
  • 我这样做是因为我希望它在每次循环时清除列表,所以如果这个人想要一个真/假问题,他们可以留两个空白。
  • 这没有任何意义。如果他们将第三个答案留空,您将把 Answer[3] 设置为 '';如果他们不将其留空,您将其设置为他们键入的任何内容。无论哪种方式,事先将其设置为'3' 有什么意义,创建一个完全不同的空列表,将其简要分配给Answer,然后将其丢弃以用不同的列表替换它的意义何在?

标签: python


【解决方案1】:

__init__ 方法签名是 __init__,而不是 Python 中的 _init_

【讨论】:

    【解决方案2】:

    值得学习如何自己调试。

    您的错误消息中没有任何内容表明该问题与 sqlite3 有任何关系。那么,如果取出QuestionStorage 中的所有sqlite3 调用并运行程序会发生什么?

    class QuestionStorage(object):
        def _init_(self, path):
            self.connection = None
            self.cursor = None
    
        def Close(self):
            pass
    
        def CreateDb(self):
            query = """CREATE TABLE questions
                     (id INTEGER PRIMARY KEY, Question TEXT, Answer1 TEXT, Answer2 TEXT, Answer3 TEXT, Answer4 TEXT, CorrectAnswer TEXT)"""
    
        def AddQuestion(self, Question, Answer1, Answer2, Answer3, Answer4):
            pass
    
        def GetQuestion(self, index = None):
            return [""]
    

    你得到完全相同的错误。这证明 sqlite3 不是罪魁祸首,希望能给您提供如何调试它的线索。

    您可以看到问题发生在从回溯创建 QuestionStorage 时,而您只在程序中执行了一次……那么如果您从其余代码中取出所有其他内容呢?

    class QuestionStorage(object):
        def _init_(self, path):
            pass
    
    qs = QuestionStorage("questions.db")
    

    同样的错误。现在更明显的是问题所在。而且,即使您自己无法弄清楚,您也可以将 5 行精简版发布到 SO。

    【讨论】:

      【解决方案3】:

      我相信您需要在 init 前面加上两个下划线,在 init 后面加上两个下划线,而不仅仅是一个。

      【讨论】:

        猜你喜欢
        • 2016-04-03
        • 1970-01-01
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 2015-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多