I understand that static methods are meant to perform operations that do not access methods or attributes within the same class. They simply perform specific operations that do not need the class reference, like building a string with a certain pattern, for example. Class methods, on the other hand, are meant to access methods and attributes of its own class.
However, is there any practical difference between accessing methods/attributes through a @staticmethod
or @classmethod
? See the two constructions below.
Getting/Setting via @staticmethod
class Foo:
bar= 1
@staticmethod
def set_bar_static(val):
Foo.bar = val
@staticmethod
def get_bar_static(val):
return Foo.bar
Getting/Setting via @classmethod
class Foo:
bar= 1
@classmethod
def set_bar(cls, val):
cls.bar= val
@classmethod
def get_bar(cls, val):
return cls.bar