Difference between staticmethod and classmethod

后端 未结 28 2196
一整个雨季
一整个雨季 2020-11-21 06:11

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?

28条回答
  •  一个人的身影
    2020-11-21 06:43

    To decide whether to use @staticmethod or @classmethod you have to look inside your method. If your method accesses other variables/methods in your class then use @classmethod. On the other hand, if your method does not touches any other parts of the class then use @staticmethod.

    class Apple:
    
        _counter = 0
    
        @staticmethod
        def about_apple():
            print('Apple is good for you.')
    
            # note you can still access other member of the class
            # but you have to use the class instance 
            # which is not very nice, because you have repeat yourself
            # 
            # For example:
            # @staticmethod
            #    print('Number of apples have been juiced: %s' % Apple._counter)
            #
            # @classmethod
            #    print('Number of apples have been juiced: %s' % cls._counter)
            #
            #    @classmethod is especially useful when you move your function to other class,
            #       you don't have to rename the class reference 
    
        @classmethod
        def make_apple_juice(cls, number_of_apples):
            print('Make juice:')
            for i in range(number_of_apples):
                cls._juice_this(i)
    
        @classmethod
        def _juice_this(cls, apple):
            print('Juicing %d...' % apple)
            cls._counter += 1
    

提交回复
热议问题