Difference between staticmethod and classmethod

后端 未结 28 3054
一整个雨季
一整个雨季 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:50

    My contribution demonstrates the difference amongst @classmethod, @staticmethod, and instance methods, including how an instance can indirectly call a @staticmethod. But instead of indirectly calling a @staticmethod from an instance, making it private may be more "pythonic." Getting something from a private method isn't demonstrated here but it's basically the same concept.

    #!python3
    
    from os import system
    system('cls')
    # %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %
    
    class DemoClass(object):
        # instance methods need a class instance and
        # can access the instance through 'self'
        def instance_method_1(self):
            return 'called from inside the instance_method_1()'
    
        def instance_method_2(self):
            # an instance outside the class indirectly calls the static_method
            return self.static_method() + ' via instance_method_2()'
    
        # class methods don't need a class instance, they can't access the
        # instance (self) but they have access to the class itself via 'cls'
        @classmethod
        def class_method(cls):
            return 'called from inside the class_method()'
    
        # static methods don't have access to 'cls' or 'self', they work like
        # regular functions but belong to the class' namespace
        @staticmethod
        def static_method():
            return 'called from inside the static_method()'
    # %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %
    
    # works even if the class hasn't been instantiated
    print(DemoClass.class_method() + '\n')
    ''' called from inside the class_method() '''
    
    # works even if the class hasn't been instantiated
    print(DemoClass.static_method() + '\n')
    ''' called from inside the static_method() '''
    # %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %   %
    
    # >>>>> all methods types can be called on a class instance <<<<<
    # instantiate the class
    democlassObj = DemoClass()
    
    # call instance_method_1()
    print(democlassObj.instance_method_1() + '\n')
    ''' called from inside the instance_method_1() '''
    
    # # indirectly call static_method through instance_method_2(), there's really no use
    # for this since a @staticmethod can be called whether the class has been
    # instantiated or not
    print(democlassObj.instance_method_2() + '\n')
    ''' called from inside the static_method() via instance_method_2() '''
    
    # call class_method()
    print(democlassObj.class_method() + '\n')
    '''  called from inside the class_method() '''
    
    # call static_method()
    print(democlassObj.static_method())
    ''' called from inside the static_method() '''
    
    """
    # whether the class is instantiated or not, this doesn't work
    print(DemoClass.instance_method_1() + '\n')
    '''
    TypeError: TypeError: unbound method instancemethod() must be called with
    DemoClass instance as first argument (got nothing instead)
    '''
    """
    

提交回复
热议问题