Is @staticmethod decorator needed for declaring a Static Method in Python?

You need the decorator if you intend to try to call the @staticmethod from the instance of the class instead of of the class directly

class Foo():
    def bar(x):
        return x + 5

>>> f = Foo()
>>> f.bar(4)
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    f.bar(4)
TypeError: bar() takes 1 positional argument but 2 were given

Now if I declare @staticmethod the self argument isn’t passed implicitly as the first argument

class Foo():
    @staticmethod
    def bar(x):
        return x + 5

>>> f = Foo()
>>> f.bar(4)
9

Leave a Comment