0

Note: this question is not about

  • "How can I use @classmethod and @staticmethod?" or
  • "What is the difference between @staticmethod and @classmethod?"

In the following classes,

  1. both @classmethod and @staticmethod can access a class attribute.
  2. neither of them can access instance attributes.

MyClass1.py

class MyClass1:
    __module_attr: str = "initial text value"

    def __init__(self):
        self.__instance_attr: str = "instance text value"

    @classmethod
    def cls_modify(cls):
        cls.__module_attr = "class text value"

    @staticmethod
    def stat_display():
        print(MyClass1.__module_attr)


if __name__ == '__main__':
    my_obj = MyClass1()
    my_obj.stat_display()
    my_obj.cls_modify()
    my_obj.stat_display()

MyClass2.py

class MyClass2:
    module_attr: str = "initial text value"

    def __init__(self):
        self.__instance_attr: str = "instance text value"

    @classmethod
    def cls_modify(cls):
        cls.module_attr = "class text value"

    @staticmethod
    def stat_display():
        print(MyClass2.module_attr)


if __name__ == '__main__':
    my_obj = MyClass2()
    my_obj.stat_display()
    my_obj.cls_modify()
    my_obj.stat_display()
    print(my_obj.module_attr)
    print(MyClass2.module_attr)

So, I can't see any advantage of one type over another.

When should I prefer @classmethod over a @staticmethod and vice versa?

user366312
  • 16,949
  • 65
  • 235
  • 452
  • 2
    The difference is that the class method gets the class as the first argument, whereas you hardcode the class name in the static method. This makes a difference for inheritance. – deceze Apr 29 '21 at 05:13
  • My rule of thumb is: if it uses neither `self` nor the class, then: 1) is it really a method or rather an independent function? And 2) if it still makes sense as a method, then mark it as `@staticmethod` to make it explicit that it uses neither. In every other case, it’s a regular method or a class method. – deceze Apr 29 '21 at 05:21
  • @deceze, what if the method accesses a class-attribute, but no instance attribute? – user366312 Apr 29 '21 at 05:31
  • Is it supposed to be *called on the class*? Then it’s by necessity a class method. If it’s supposed to be called on an instance, then it’s a normal method. I can’t really think of a useful example of an instance method that only manipulates the class though. You’ll probably know it when you need it. – deceze Apr 29 '21 at 05:48
  • @deceze, I need to store a dictionary in a class where each instance of the class should be able to access the dictionary. The dictionary contains hundreds of objects. As a result, that dictionary is memory-intensive. So, I can keep the dictionary as a private class attribute and access that using either a `classmethod` or a `staticmethod`. Which type should I use? – user366312 Apr 29 '21 at 06:07
  • You can access class attributes just fine from instances as well, that in itself is no criterion whatsoever. – deceze Apr 29 '21 at 13:16

0 Answers0