问答详情
源自:2-3 Python实例属性的定义

为什么一直是失败呢?

class animal(object):

    pass dog=animal()cat=animal()

    dog.name="xiaohuang"

    dog.age='1'

    cat.name='xiaohua'

    cat.age='1.5'

    print (dog.name)

    print (dog.age)

    print (cat.name)

    print (cat.age)


提问者:arthor 2021-05-07 22:35

个回答

  • timworld
    2021-05-21 18:57:39

    class animal(object):    
        pass     
    dog=animal()
    cat=animal()
    dog.name="xiaohuang"
    dog.age='1'
    cat.name='xiaohua'
    cat.age='1.5'
    print (dog.name)
    print (dog.age)
    print (cat.name)
    print (cat.age)


  • timworld
    2021-05-21 18:54:50

    class animal(object):    pass     dog=animal()cat=animal()dog.name="xiaohuang"dog.age='1'cat.name='xiaohua'cat.age='1.5'print (dog.name)print (dog.age)print (cat.name)print (cat.age)


  • 慕哥7358830
    2021-05-12 08:46:12

    print后面有空格

     print(dog.name)
     print(dog.age)
     print(cat.name)