Search code examples
pythonstatic-methods

(Python) Is there any advantage to using staticmethods?


I was writing a function earlier when my linter recommended I made the method static. I've always used staticmethods without really knowing what they're for or why they're useful, besides from I can't access any attributes of the current class (since there's no self)

I was just wandering, is there any advantage doing this

class Foo:
    @staticmethod
    def bar():
        return "bazz"

over just doing

class Foo:
    def bar(self):
        return "bazz"

Thank you.


Solution

  • At least the calling of static method does not require to pass a "self" parameter, so - yes, it has an advantage.