问答详情
源自:2-5 Python类属性

请问为什么会运行失败呢

calss Anmial(object):

    count = 0

    def_init_(self,name,age):

        self.name = name

        self.age = age

        Animal.count += 1

dog = Animal('wangwang',1)

print(Animal.count)

cat = Animal('mimi',3)

print(Animal.count)

pig = Animal('panpan',1)

print(Animal.count)


提问者:20044031 2021-01-22 19:45

个回答

  • 慕哥2041944
    2021-01-23 10:40:45
    已采纳

      Animal.count += 1 改成   Animal.count =Animal.count + 1

  • weixin_慕无忌7494868
    2021-05-26 10:04:44

    init的前后是两个_ ,不是一个_

  • 疯狂圣诞
    2021-04-20 17:15:17

    class Anmial(object):

    count = 0

    def __init__(self,name,age):

    self.name=name

    self.age=age 

    Anmial.count+=1   

    dog = Anmial('wangwang',1)

    print(Anmial.count)


    cat = Anmial('mimi',3)


    print(Anmial.count)


    pig = Anmial('panpan',1)


    print(Anmial.count)


  • 疯狂圣诞
    2021-04-20 17:10:33

    class打错了   Anmial 打成Animal 能运行就怪了