When should a static method be a function?

Question:

I am writing a class for an image processing algorithm which has some methods, and notably a few static methods. My IDE keeps telling me to convert static methods to function which leads me to the following question:

When should a static method be turned into a function? When shouldn’t it?

Asked By: arnino

||

Answers:

There are no set rules in python regarding this decision, but there are style-guides defined e.g. by companies that look to solve the ambiguity of when to use what. One popular example of this would be the Google Python Style Guide:

Never use staticmethod unless forced to in order to integrate with an API defined in an existing library. Write a module level function instead.

My guess is, that your IDE follows this stance of a hard no against the staticmethod. If you decide, that you still want to use staticmethods, you can try to disable the warning by adding # noqa as a comment on the line where the warning is shown. Or you can look in your IDE for a setting to disable this kind of warning globally.

But this is only one opinion. There are some, that do see value in using staticmethods (staticmethod considered beneficial, Why Python Developers Should Use @staticmethod and @classmethod), and there are others that argue against the usage of staticmethods (Thoughts On @staticmethod Usage In Python, @staticmethod considered a code smell)

Another quote that is often cited in this discussion is from Guido van Rossum (creator of Python):

Honestly, staticmethod was something of a mistake — I was trying to
do something like Java class methods but once it was released I found
what was really needed was classmethod. But it was too late to get rid
of staticmethod.


I have compiled a list of arguments that I found, without any evaluation or order.

Pro module-level function:

  • Staticmethod lowers the cohesion of the class it is in as it is not using any of the attributes the class provides.

  • To call the staticmethod any other module needs to import the whole class even if you just want to use that one method.

  • Staticmethod binds the method to the namespace of the class which makes it longer to write SomeWhatDescriptiveClassName.method instead of method and more work to refactor code if you change the class.

  • Easier reuse of method in other classes or contexts.

  • The call signature of a staticmethod is the same as that of a classmethod or instancemethod. This masks the fact that the staticmethod does not actually read or modify any object information especially when being called from an instance. A module-level function makes this explicit.

Pro staticmethod:

  • Being bound by an API your class has to work in, it can be the only valid option.

  • Possible usage of polymorphism for the method. Can overwrite the staticmethod in a subclass to change behaviour.

  • Grouping a method directly to a class it is meant to be used with.

  • Easier to refactor between classmethod, instancemethod and staticmethod compared to module-level functions.

  • Having the method under the namespace of the class can help with reducing possible namespace-collisions inside your module and reducing the namespace of your module overall.

As I see it, there are no strong arguments for or against the staticmethod (except being bound by an API). So if you work in an organisation that provides a code standard to follow, just do that. Else it comes down to what helps you best to structure your code for maintainability and readability, and to convey the message of what your code is meant to do and how it is meant to be used.

Answered By: MangoNrFive