[英]How can I use __str__ to change how an attribute of my objects shows up?
I want the chips my player has to show up as $xxx, but to keep the chips as an int.我希望我的玩家的筹码必须显示为 $xxx,但要将筹码保留为 int。 I tried the code below, but the str part is just grey.我尝试了下面的代码,但str部分只是灰色。 What am I doing wrong?我究竟做错了什么?
class Player:
def __init__(self, name, gender, taunt, aggression, hand, chips):
self.name = name
self.gender = gender
self.taunt = taunt
self.aggression = aggression
self.hand = hand
self.chips = chips
def __str__(self):
return "${0}".format(self.chips)
What you need to do is unindent the __str__
function and it will work as normal:你需要做的是__str__
函数的缩进,它会正常工作:
class Player:
def __init__(self, name, gender, taunt, aggression, hand, chips):
self.name = name
self.gender = gender
self.taunt = taunt
self.aggression = aggression
self.hand = hand
self.chips = chips
# Note the outdent of the function
def __str__(self):
return "${0}".format(self.chips)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.