1

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)?

Milan
  • 1,743
  • 2
  • 13
  • 36
RobertR
  • 745
  • 9
  • 27
  • Is your method *classmethod*, *staticmethod* or *instance method*? – styvane Apr 13 '16 at 05:32
  • @user3100115 It's an instance method, hence why it's getting `self` passed to it. – RobertR Apr 13 '16 at 05:35
  • Possible duplicate of [What is the purpose of self in Python?](http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python) – styvane Apr 13 '16 at 05:36
  • @user3100115 I'm well aware of what `self` actually does. I'm wondering what to do if it's in an instance method but isn't actually being used. Should I just ignore it or make the method a static method. It's really a question of what is the conventional thing to do in this case. – RobertR Apr 13 '16 at 05:43
  • Instance methods generally refer to the instance itself as `self`. If you omit `self`, PyCharm notifies you so that you can add either `self` (to match convention) or `@staticmethod` (to make it no longer an instance method). If you've seen and ignored this warning "many times," you're writing Python code that will be more difficult for the next reader to understand. – TigerhawkT3 Apr 13 '16 at 05:44
  • Do you have an example? All I can think of right now are either constant methods or random methods. The first type I'd make constants rather than instance methods, and the second type I'd make static. – magni- Apr 13 '16 at 05:46
  • It doesn't matter that you're not referring to the instance itself within the method. The first parameter _will be_ the instance itself. If you try to define it with no parameters at all, you'll get an error when Python tries to pass in the instance. If you've been assuming that simply omitting a parameter called `self` is the way to create a static method, any affected code is probably broken. – TigerhawkT3 Apr 13 '16 at 05:46
  • Here, read and study this: https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods – Reblochon Masque Apr 13 '16 at 05:50
  • The question whether a method should be an instance method or static is purely opinion based. For example IMHO if you don't use `self` then you should not put the method on a class to begin with. Unless inheritance kicks in (i.e. for some subclass the overwritten method is independent of an instance). So I'm voting to close it. – freakish Apr 13 '16 at 06:13

1 Answers1

3

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.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284