【问题标题】:Simple Class not working python [closed]简单类不工作python [关闭]
【发布时间】:2016-03-07 03:45:25
【问题描述】:

无法让一个简单的类示例工作,我不知道为什么。 为什么我的对象没有属性?

class Player(object):

    def _init_(self, name="n", hp=25):
        self.name = name
        self.hp = hp
    def take_hit(self):
        self.hp -= 5

dude= Player()
print (dude.name)
dude.take_hit
print (dude.hp)

【问题讨论】:

  • __init__dude.take_hit()

标签: python class python-3.x


【解决方案1】:

__init__ 和所有 Python 魔术方法一样,需要在每一边有两个下划线,而不是一个。

【讨论】:

  • 哦,非常感谢!该死的语法...
猜你喜欢
  • 2014-05-08
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2017-10-16
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多