自定义 def __str__(self): 在 Python 中创建 class 时的方法
Customize def __str__(self): method when creating a class in Python
我正在 Python3 中创建一个 MultipleChoice class,其中第一个参数应该是一个问题,第二个参数是一个包含选项的列表(每个条目都是一个字符串,没有编号),第三个参数是正确答案的个数。包含备选方案的列表可能有不同的长度。
我已经创建了 class,我正在尝试创建一个 _str_ 方法,returns 问题和下面的所有备选方案问题,但这次编号。我想要实现的目标大纲如下所示:
question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
print(question1)
print(question2)
预期输出:
什么最好?
1 - Mac
, 2 - 电脑
英格兰最大的城市?
1 - 曼彻斯特
, 2 - 利物浦
, 3 - 伦敦
我曾尝试将方法插入 _str_ 方法中 f 字符串的括号中,但未成功。我这样做是因为我假设我必须用备选方案遍历列表并为每个条目分配一个类似于索引号的数字,并且认为像这样更复杂的任务最好用单独的方法来完成(而不是尝试这样做在 f 字符串的括号内。
这也是一项学校作业,所以我必须使用 _str_ 方法来实现。
如有任何帮助,我们将不胜感激。以下是我目前的代码:
class MultipleChoice:
#Constructor
def __init__(self, question, alternatives, correct_answer):
self.question = question
self.alternatives = alternatives
self.correct_answer = correct_answer
def __str__(self):
return f'{self.question}? \n{self.alternatives}.'
if __name__ == '__main__':
question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
print(question1)
print(question2)
您可以尝试使用 __str__
,如下所示(使用破折号 and/or 空格而不是换行符更新 f 字符串以满足您的需要):
>>> class MultipleChoice:
... #Constructor
... def __init__(self, question, alternatives, correct_answer):
... self.question = question
... self.alternatives = alternatives
... self.correct_answer = correct_answer
... def __str__(self):
... return f'{self.question}\n' + '\n'.join(f'{i} {a}' for i, a in enumerate(self.alternatives,1))
...
>>> question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
>>> question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
>>>
>>> print(question1)
What´s best?
1 Mac
2 PC
>>> print(question2)
Biggest city in England?
1 Manchester
2 Liverpool
3 London
为此,我们可以使用 enumerate 轻松获取列表中给定元素的索引号,使用它的第二个参数,我们可以将其从 1 开始,我们可以使用它来获取
>>> question = 'Biggest city in England?'
>>> alternatives = ['Manchester', 'Liverpool', 'London']
>>> for i,v in enumerate(alternatives,1):
print(f'{i} - {v}')
1 - Manchester
2 - Liverpool
3 - London
>>>
现在让我们把之前的循环改成合适的list comprehension
>>> [f'{i} - {v}' for i,v in enumerate(alternatives,1)]
['1 - Manchester', '2 - Liverpool', '3 - London']
>>>
现在我们使用str.join to, well, join them together and remove the [] to make it into a generator expression,而我们
>>> temp=', '.join(f'{i} - {v}' for i,v in enumerate(alternatives,1))
>>> temp
'1 - Manchester, 2 - Liverpool, 3 - London'
>>>
现在我们已经完成了 80%,让我们进行最后的润色
>>> f"{question} {temp}"
'Biggest city in England? 1 - Manchester, 2 - Liverpool, 3 - London'
>>>
我们完成了。
此外,f-string 几乎可以采用任何 python 表达式,因此我们可以将其变成单行
>>> f"{question} { ', '.join(f'{i} - {v}' for i,v in enumerate(alternatives,1)) }"
'Biggest city in England? 1 - Manchester, 2 - Liverpool, 3 - London'
>>>
我正在 Python3 中创建一个 MultipleChoice class,其中第一个参数应该是一个问题,第二个参数是一个包含选项的列表(每个条目都是一个字符串,没有编号),第三个参数是正确答案的个数。包含备选方案的列表可能有不同的长度。
我已经创建了 class,我正在尝试创建一个 _str_ 方法,returns 问题和下面的所有备选方案问题,但这次编号。我想要实现的目标大纲如下所示:
question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
print(question1)
print(question2)
预期输出:
什么最好? 1 - Mac , 2 - 电脑
英格兰最大的城市? 1 - 曼彻斯特 , 2 - 利物浦 , 3 - 伦敦
我曾尝试将方法插入 _str_ 方法中 f 字符串的括号中,但未成功。我这样做是因为我假设我必须用备选方案遍历列表并为每个条目分配一个类似于索引号的数字,并且认为像这样更复杂的任务最好用单独的方法来完成(而不是尝试这样做在 f 字符串的括号内。
这也是一项学校作业,所以我必须使用 _str_ 方法来实现。
如有任何帮助,我们将不胜感激。以下是我目前的代码:
class MultipleChoice:
#Constructor
def __init__(self, question, alternatives, correct_answer):
self.question = question
self.alternatives = alternatives
self.correct_answer = correct_answer
def __str__(self):
return f'{self.question}? \n{self.alternatives}.'
if __name__ == '__main__':
question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
print(question1)
print(question2)
您可以尝试使用 __str__
,如下所示(使用破折号 and/or 空格而不是换行符更新 f 字符串以满足您的需要):
>>> class MultipleChoice:
... #Constructor
... def __init__(self, question, alternatives, correct_answer):
... self.question = question
... self.alternatives = alternatives
... self.correct_answer = correct_answer
... def __str__(self):
... return f'{self.question}\n' + '\n'.join(f'{i} {a}' for i, a in enumerate(self.alternatives,1))
...
>>> question1 = MultipleChoice('What´s best?', ['Mac', 'PC'], 1)
>>> question2 = MultipleChoice('Biggest city in England?' , ['Manchester','Liverpool', 'London'], 3)
>>>
>>> print(question1)
What´s best?
1 Mac
2 PC
>>> print(question2)
Biggest city in England?
1 Manchester
2 Liverpool
3 London
为此,我们可以使用 enumerate 轻松获取列表中给定元素的索引号,使用它的第二个参数,我们可以将其从 1 开始,我们可以使用它来获取
>>> question = 'Biggest city in England?'
>>> alternatives = ['Manchester', 'Liverpool', 'London']
>>> for i,v in enumerate(alternatives,1):
print(f'{i} - {v}')
1 - Manchester
2 - Liverpool
3 - London
>>>
现在让我们把之前的循环改成合适的list comprehension
>>> [f'{i} - {v}' for i,v in enumerate(alternatives,1)]
['1 - Manchester', '2 - Liverpool', '3 - London']
>>>
现在我们使用str.join to, well, join them together and remove the [] to make it into a generator expression,而我们
>>> temp=', '.join(f'{i} - {v}' for i,v in enumerate(alternatives,1))
>>> temp
'1 - Manchester, 2 - Liverpool, 3 - London'
>>>
现在我们已经完成了 80%,让我们进行最后的润色
>>> f"{question} {temp}"
'Biggest city in England? 1 - Manchester, 2 - Liverpool, 3 - London'
>>>
我们完成了。
此外,f-string 几乎可以采用任何 python 表达式,因此我们可以将其变成单行
>>> f"{question} { ', '.join(f'{i} - {v}' for i,v in enumerate(alternatives,1)) }"
'Biggest city in England? 1 - Manchester, 2 - Liverpool, 3 - London'
>>>