问答详情
源自:2-9 Python定义类方法

File "index.py", line 9 @classmethod ^ IndentationError: unindent does not match any outer indentation level,报错信息,不知道如何解决,请解决下,谢谢!

class Animal(object):

     __count=0

     def __init__(self,name,age,gender):

         self.name=name

         self.age=age

         self.gender=gender

          

    @classmethod

    def set_count(cls):

        cls.__count+=1

    

    @classmethod

    def get_count(cls):

        return cls.__count

        

dog=Animal('littledog',2,'male') 

Animal.set_count()

cat=Animal('littlecat',3,'female')

Animal.set_count()

print(Animal.get_count())


提问者:慕无忌6336060 2021-04-01 21:53

个回答

  • weixin_慕桂英3100311
    2022-05-12 16:51:06

         def __init__(self,name,age,gender):

             self.name=name

             self.age=age

             self.gender=gender

    这几行的缩进,比@classmethod的缩进多一个空格

  • 慕前端5244970
    2021-10-20 20:11:38

    http://img2.mukewang.com/617007670001dad410151160.jpg

    代码没问题,注意缩进即可

  • 幕布斯6178042
    2021-04-25 18:05:26

    class Animal(object):

        

        __count=0


        def __init__(self,name,age,gender):


             self.name=name


             self.age=age


             self.gender=gender


              


        @classmethod


        def set_count(cls):


            cls.__count+=1


        


        @classmethod


        def get_count(cls):


            return cls.__count


            


    dog=Animal('littledog',2,'male') 


    Animal.set_count()


    cat=Animal('littlecat',3,'female')


    Animal.set_count()


  • 慕无忌6336060
    2021-04-04 11:19:22

    慕勒4078882,请标识下哪儿缩进不对,请标识下,谢谢了

  • 慕勒4078882
    2021-04-03 18:26:10

    你class里缩进不对,没完全对齐。

    # Enter a code

    class Animal(object):

        __count=0

        def __init__(self,name,age,gender):

            self.name=name

            self.age=age

            self.gender=gender

        @classmethod

        def set_count(cls):

            cls.__count+=1

        @classmethod

        def get_count(cls):

            return cls.__count

    dog=Animal('littledog',2,'male') 


    Animal.set_count()


    cat=Animal('littlecat',3,'female')


    Animal.set_count()


    print(Animal.get_count())