Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何调用类型为(type)的类_Python_Class - Fatal编程技术网

Python 如何调用类型为(type)的类

Python 如何调用类型为(type)的类,python,class,Python,Class,我正在做一个游戏,在代码中有两个类。一个定义问题,另一个定义4个选择题答案。这就是我所拥有的: class new_question(type): """ The Question that displays on screen """ def __init__(self, question): super(new_question, self).__init__(question = question) def ask_quest(self):

我正在做一个游戏,在代码中有两个类。一个定义问题,另一个定义4个选择题答案。这就是我所拥有的:

class new_question(type):
""" The Question that displays on screen """

    def __init__(self, question):
        super(new_question, self).__init__(question = question)

    def ask_quest(self):
        global QUESTION
        QUESTION = ask_question
        QUESTION.value = question
那是我的第一节课,第二节课是:

class answer(type):
""" Four answers that display in their own boxes """

    def __init__(self, answers):
        super(answer, self).__init__(answers = answers)

    def all_answers(self):
        global ANS1
        global ANS2
        global ANS3
        global ANS4
        ANS1 = poss_ans_1
        ANS1.value = answers[0]
        ANS2 = poss_ans_2
        ANS2.value = answers[1]
        ANS3 = poss_ans_3
        ANS3.value = answers[2]
        ANS4 = poss_ans_4
        ANS4.value = answers[3]
所有变量都定义在这个文件的其他地方,以及其他文件中,但这不是我遇到的问题。当我去调用这些类时,我认为最好的方法是从我的主循环中的类中调用单个函数:

def main():
    load_image()
    ans = answer(type)
    ans.all_answers()

main()
但是,当我运行程序时,会出现以下错误:

Traceback (most recent call last):
File "C:\Users\Roger\Documents\Trivia New\main.py", line 83, in <module>
main()
File "C:\Users\Roger\Documents\Trivia New\main.py", line 82, in main
ans.all_answers()
AttributeError: type object 'type' has no attribute 'all_answers'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Roger\Documents\Trivia New\main.py”,第83行,在
main()
文件“C:\Users\Roger\Documents\Trivia New\main.py”,第82行,main
答:所有答案()
AttributeError:type对象“type”没有属性“all\u answers”

我不确定到底发生了什么,但我已经在这个问题上解决了3个小时了,仍然无法解决它。如果有人能帮助我,我将不胜感激。

您的类应该是
对象
的子类,而不是
类型


子类化
type
使您的类成为元类—类的类。

我认为您应该调用ans=answer(type,answers),其中answers是您的答案列表,因为answer没有带单个参数的构造函数。在类方法中使用
global
打破了封装和代码重用的所有想法。尝试在不使用
global
的情况下编码。它起作用了。真正地