我在做一个游戏,在代码中有两个类。一个定义问题,另一个定义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'我不知道发生了什么,但是我已经在这个问题上做了3个小时了,仍然不能解决它。如果有人能帮助我,我将不胜感激。
发布于 2013-07-11 16:16:40
类应该是object的子类,而不是type的子类。
对type进行子类化使您的类成为元类-类的类。
https://stackoverflow.com/questions/17588157
复制相似问题