【发布时间】:2012-02-08 00:48:15
【问题描述】:
我需要从具有.__str__() 方法的类列表中获取一个字符串。
所以 potions 是 Potion 类的对象列表。 Potion __str__ 方法只返回药水的名称。
我想过做这样的事情
result = "\n".join(potions)
但只能加入字符串,我不知道如何为加入中的每个类调用__str__()。
或者我应该这样做:
for potion in potions:
result += "{0}\n".format(potion)
或者可能是别的什么?
【问题讨论】:
标签: python string class join generator