【问题标题】:Compiling Python Class Objects into a List将 Python 类对象编译成列表
【发布时间】:2023-03-10 10:45:01
【问题描述】:

我正在尝试学习如何将一个类的所有成员自动编译成一个列表。这段代码不是真实项目的一部分,只是帮助我解释我的目标的一个例子。我似乎找不到任何关于此的阅读材料,我什至不知道这是否可能。提前感谢您的回答! =)

class question:
    def __init__(self,question,answer,list_of_answers):
        self.question=question
        self.answer=answer
        self.list_of_answers=list_of_answers

question_01=question('''
Which of these fruits is red?
A). Banana
B). Orange
C). Apple
D). Peach
''',"C",("A","B","C","D"))

question_02=question('''
Which of these is both a fruit and a vegetable?
A). Cauliflower
B). Tomato
C). Brocolli
D). Okrah
''',"B",("A","B","C","D"))

'''My objective is to write code that can automatically compile my questions (the
members of my question class) into a list,even if I have hundreds of them, without
having to manually write them into a list.'''

#If there are only two questions, final output should automatically become:
all_questions=[question_01,question_02]

#If there are one hundred questions, final output should automatically become:
all_questions=[question_01,question_02, ... ,question_99,question_100]

#Without having to manually type all of the one hundred questions (or members
#of the question class) to the list.

【问题讨论】:

  • 为什么不创建一个列表并附加对象?
  • 我认为“编译”这个词是正确的。
  • 我可以假设您的输入是一个问题列表,例如: [q1, q2, ... q100] 其中 q1 = "这些水果中哪些是红色的?",q2 = "其中哪些是既是水果又是蔬菜?”等;和 [a1,a2, ... an] 其中 a1 =["Banana", "Orange", "Apple", "Peach"], ... 等等。不清楚您的输入和输出是什么.
  • @juanpa.arrivillaga 干得好!这对我来说是完美的!
  • @mm_ 实际上,我正在寻找一个作为输出的列表。基本上,我想告诉程序“取出属于这个特定类的每个对象,并从中列出一个列表。我可以按照 juanpa.arrivillaga 的建议分别附加每个对象,它会很好用,但我是还希望使用单个命令为该类创建所有对象的列表。=)

标签: python list class oop


【解决方案1】:

首先,您不应该有 100 个 question_01question_100 变量。当您想重新排列问题、删除一个问题或在中间添加一个问题时,您会遇到麻烦。当您想在question_02question_03 之间提出一个新问题时,您真的要重命名 98 个变量吗?

此时,您应该强烈考虑将问题放入与源代码分开的数据文件中,并从文件中读取问题。但是,即使您不这样做,您也应该消除编号变量。将问题放在列表中开始。 (另外,类应该用 CamelCase 命名):

questions = [
    Question('''
Which of these fruits is red?
A). Banana
B). Orange
C). Apple
D). Peach
''', "C", ("A","B","C","D")),
    Question('''
Which of these is both a fruit and a vegetable?
A). Cauliflower
B). Tomato
C). Brocolli
D). Okrah
''', "B", ("A","B","C","D")),
    ...
]

【讨论】:

  • 好资料!我对创建和阅读文本文件不是很了解,但我想我会接受挑战并学习如何。感谢您的建议!顺便说一句,我会稍等片刻,然后将您的答案投票为最好的,如果没有人能顶它! =)
【解决方案2】:

有一种方法可以做你想做的事:从模块(或文件)中获取给定类型的所有对象的列表。我提出了两种解决方案:

选项一,来自不同的模块(文件):

假设你有以下文件:

问题模块.py

 class question:
    def __init__(self,question,answer,list_of_answers):
    self.question=question
    self.answer=answer
    self.list_of_answers=list_of_answers

 question_01=question('''
 Which of these fruits is red?
 A). Banana
 B). Orange
 C). Apple
 D). Peach
 ''',"C",("A","B","C","D"))

 question_02=question('''
 Which of these is both a fruit and a vegetable?
 A). Cauliflower
 B). Tomato
 C). Brocolli
 D). Okrah
 ''',"B",("A","B","C","D"))

然后您可以通过以下方式获取所有问题:

GetQuestions.py

import QuestionModule
def get():
    r = []
    for attribute in dir(QuestionModule):
        #print(attribute,"  ",type(getattr(QuestionModule,attribute)))
        if type(getattr(QuestionModule,attribute)) == QuestionModule.question:
            r.append(getattr(QuestionModule,attribute))  
    return r

l_questions = get()

或者:

import GetQuestions
l_questions = GetQuestions.get()

选项二,来自同一个模块(文件):

如果你想对同一个文件做同样的事情,你可以这样做:

 class question:
    def __init__(self,question,answer,list_of_answers):
    self.question=question
    self.answer=answer
    self.list_of_answers=list_of_answers

 question_01=question('''
 Which of these fruits is red?
 A). Banana
 B). Orange
 C). Apple
 D). Peach
 ''',"C",("A","B","C","D"))
 question_02=question('''
 Which of these is both a fruit and a vegetable?
 A). Cauliflower
 B). Tomato
 C). Brocolli
 D). Okrah
 ''',"B",("A","B","C","D"))

def getQuestions():
    import sys
    l = dir(sys.modules[__name__])
    r = []
    for e in l:
        if sys.modules[__name__].question==type(getattr(sys.modules[__name__],e)):
            r.append(getattr(sys.modules[__name__], e))
    return r

L = getQuestions()
for i in L :
    print(i)
    print(i.question)

如果要多次调用getQuestions,可以从方法中取出import sys放在顶部。

【讨论】:

  • 哇!你的代码比我目前对 Python 的理解更高级,但是在修改它之后,我可以告诉你找到了解决方案。现在我只需要弄清楚它是如何工作的,哈哈。谢谢哥们!
【解决方案3】:

两个答案都很好。我希望我可以选择他们两个作为主要答案。我把它给了 mm_ 因为他的回答最符合我的目标,但我也很喜欢 user2357112 的回答。

感谢大家的回答!

【讨论】:

  • 如果你给我一个电子邮件地址,我可以告诉你它是如何工作的。如果您仍然感兴趣。
猜你喜欢
  • 2021-02-25
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
相关资源
最近更新 更多