emufossum13
emufossum13

Reputation: 407

How can I call a class with type (type)

I'm making a game, and in the code two classes. One that defines the question, and the other that defines the 4 multiple choice answers. This is what I have:

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

That is my first class, and my second class is:

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]

All the variables are defined elsewhere in this file, and in others, but that's not the problem I'm having. When I go to call these classes I assume the best thing to do would be to call the individual function from the class in my main loop here:

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

main()

However, when I run the program, I get this error:

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'

I'm not sure what's going on, but I've been at this same problem for 3 hours now, and still can't figure it out. If someone could help me, I would appreciate it.

Upvotes: 1

Views: 93

Answers (1)

babbageclunk
babbageclunk

Reputation: 8731

Your classes should subclass object, not type.

Subclassing type makes your class a metaclass - the class of a class.

Upvotes: 3

Related Questions