【发布时间】:2015-11-03 03:44:29
【问题描述】:
playerList 包含两个Player 对象(分别调用str 属性"a" 和"b"),Player 实现__str__ 和__repr__。当我将str(playerList) 连接到另一个字符串时,我希望该字符串会附加"[a, b]" 形式的内容。相反,结果字符串会附加"[ , ]"。我犯了什么错误导致了这个结果?
这是我写的
prompt = "And then choose the opponent you would like to attack from " + str(playerList)
def __str__ (self):
return self.name
def __repr__ (self):
return str()
我在标准输出上得到了什么:
"And then choose the opponent you would like to attack from [, ]"
我想要什么:
"And then choose the opponent you would like to attack from [a,b]"
【问题讨论】:
-
返回 str() ?不应该是 self.__str__ 吗?
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
-
希望更新顺利吗?
标签: python string list concatenation