Search code examples
pythonpython-3.xmethodspycharmself

What if a method doesn't use self?


In PyCharm, whenever you declare a method that doesn't make use of the self variable it gives you the warning

Method 'method_name' may be 'static'

I've come across this warning many times and most of the time I just ignore it. However, I was wondering if there is a conventional or pythonic way to handle it.

So basically, my question is what should I do when I come across this? Should I ignore it? Should I replace it with a static method (@staticmethod)?


Solution

  • Its a hint saying that the method can be a static method since it is not acting on instances (ie, you are passing in self but aren't actually making use of it).

    There is no conventional means to handle it - either you want that method to be there, because you are creating a class tree and want it to be defined/overridden in the descendants; or for whatever other reason. In this case, you can ignore the warning.

    This is entirely different than @staticmethod; which has a lot of other consequences. So its not a matter of "if I'm not using self, but passing it in, lets just make it a static method"; you have to know what the method is doing.

    Static and class methods are most often used in factory classes.