Python:object .__ new __()不带参数

时间:2012-12-12 02:58:00

标签: python

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

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()

3 个答案:

答案 0 :(得分:10)

__init__方法签名为__init__,而非Python中的_init_

答案 1 :(得分:8)

值得学习如何自己调试。

您的错误消息中没有任何内容表明该问题与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。

答案 2 :(得分:1)

我相信在init之前需要两个下划线,在init之后需要两个下划线而不是一个。