-1

I need a function (def check) to make an instance of a class and take a list from that class.

but there are many classes and the 'key' argument passed is the number of the class I want to make an instance of.

    # if the 'key' passed is 1 then it should make an instance of Class1
    # if the 'key' passed is 2 then it should make an instance of Class2
    

    def check(self, key):        
        list1 = Class1().list1.    # it should call Class1().list1 if the key is 1 and Class2.()list1 if the key is 2
        list2 = Class1().list2
        list3 = Class1().list3

so how can I make the check() function change the class it makes an instance of?

I don't think I could make a if statement and change the class it calls depending on the key because there are many classes and it would be very repetitive to make 'if' statements for each class.

here are the classes:

class Class1:
    def __init__(self):

       self.quiz_name = 'Cities and Countries - part 1'
       self.list1 = []
       self.list2 = []     # the three lists
       self.list3 = []

       self.list1.append('Select the European City: ')
       self.list2.extend(['Abu Dhabi', 'Washington DC', 'New York', 'Rome'])
       self.list3.append(4)
       ...

class Class2:
    def __init__(self):

        self.quiz_name = 'Cities and Countries - part 1'    # the data in the lists here are different by each class, but I'm just giving an example of the classes
        self.list1 = []
        self.list2 = []
        self.list3 = []

        self.list1.append('Select the European City: ')
        self.list2.extend(['Abu Dhabi', 'Washington DC', 'New York', 'Rome'])
        self.list3.append(4)
        ...
   
  # there are more classes
coder_not_found
  • 202
  • 2
  • 13
  • 1
    The hacky way would be to look in `locals()`. The fancier way would be to `@decorate` each class definition, where the decorator adds that class to an eligible container to search for with the key lookup. – Brad Solomon Mar 16 '21 at 13:56
  • 2
    Hmm..., many classes with close (if not identical) declarations, make me think of a possible [XY problem](https://meta.stackexchange.com/q/66377/397459)... What are you really trying to achieve? BTW classes are objects in Python, so you can simply have a list (or dictionary) of classes which both allow direct access. – Serge Ballesta Mar 16 '21 at 14:03
  • this question is not similar to https://stackoverflow.com/questions/456672/class-factory-in-python I don't know why its says that – coder_not_found Mar 25 '21 at 11:55

1 Answers1

1

To answer your specific question, unless I'm missing something obvious, one way would be to create a dictionary, mapping keys to classes:

def check(self, key):
    the_list = {
        1: Class1,
        2: Class2,
        3: Class3
    }[key]().list1

However, my real suggestion would be to restructure your code. Your classes (which are poorly named, Class1 and Class2 are terrible, non-descriptive names for classes) seem like their only purpose is to create distinct quizzes. However, the quizzes aren't different in their features or interfaces - they're only different by their content. I would say these are bad candidates for classes - I don't think you can justify making separate classes for each quiz.

A single Quiz class would be more appropriate. In fact, you may not even need a class at all. Unless There's more to the code that you're not showing, you could store your quizzes in a collection of some kind. There's no strict schema you need to follow here, but let's do something reasonably JSON-y:

quizzes = [

    {
        "name": " Cities and Countries - part 1",
        "questions": [

            {
                "title": "Select the European City: ",
                "answers": [
                    "Abu Dhabi",
                    "Washington DC",
                    "New York",
                    "Rome"
                ],
                "correct_answer": "Rome"
            }

        ]
    }
    
]
Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • I used multiple classes because I think it would be harder and confusing to make different 3 lists for each question beach it'll mean that ill have to make a different name for each list in each question. anyway now I know its a terrible idea. your answer works for me but ill definitely need to basic python code for this particular program. – coder_not_found Mar 16 '21 at 14:19