The new pycharm release (3.1.3 community edition) proposes to convert the methods that don't work with the current object's state to static.
What is the practical reason for that? Some kind of micro-performance(-or-memory)-optimization?
The new pycharm release (3.1.3 community edition) proposes to convert the methods that don't work with the current object's state to static.
What is the practical reason for that? Some kind of micro-performance(-or-memory)-optimization?
PyCharm "thinks" that you might have wanted to have a static method, but you forgot to declare it to be static.
PyCharm proposes this because the method does not use self
in its body and hence does not actually change the class instance. Hence the method could be static, i.e. callable without having created a class instance before.
Agreed with @jolvi, @ArundasR, and others, the warning happens on a member function that doesn't use self
.
If you're sure PyCharm is wrong, that the function should not be a @staticmethod
, and if you value zero warnings, you can make this one go away two different ways:
Workaround #1
def bar(self):
self.is_not_used()
doing_something_without_self()
def is_not_used(self):
pass
Workaround #2 [Thanks @DavidPärsson]
# noinspection PyMethodMayBeStatic
def bar(self):
doing_something_without_self()
The application I had for this (the reason I could not use @staticmethod) was in making a table of handler functions for responding to a protocol subtype field. All handlers had to be the same form of course (static or nonstatic). But some didn't happen to do anything with the instance. If I made those static I'd get "TypeError: 'staticmethod' object is not callable".
In support of the OP's consternation, suggesting you add staticmethod whenever you can, goes against the principle that it's easier to make code less restrictive later, than to make it more -- making a method static makes it less restrictive now, in that you can call class.f() instead of instance.f().
Guesses as to why this warning exists:
I think that the reason for this warning is config in Pycharm. You can uncheck the selection Method may be static in Editor->Inspection
I can imagine following advantages of having a class method defined as static one:
remaining advantages are probably marginal if present at all:
I agree with the answers given here (method does not use self
and therefore could be decorated with @staticmethod
).
I'd like to add that you maybe want to move the method to a top-level function instead of a static method inside a class. For details see this question and the accepted answer: python - should I use static methods or top-level functions
Moving the method to a top-level function will fix the PyCharm warning, too.
This error message just helped me a bunch, as I hadn't realized that I'd accidentally written my function using my testing example player
my_player.attributes[item]
instead of the correct way
self.attributes[item]
Since you didn't refer to self
in the bar
method body, PyCharm is asking if you might have wanted to make bar
static. In other programming languages, like Java, there are obvious reasons for declaring a static method. In Python, the only real benefit to a static method (AFIK) is being able to call it without an instance of the class. However, if that's your only reason, you're probably better off going with a top-level function - as note here.
In short, I'm not one hundred percent sure why it's there. I'm guessing they'll probably remove it in an upcoming release.