Static allows you to make methods that will not change (are finalized). So if you want to give methods to another class but don't want them to change, make it a public static method.
2. Having a single implementation. Static methods are used when we don't want subclasses of a class change/override a specific implementation of a method.
Code Inspection: Method can be made 'static'Static methods normally belong to utility classes and are accessed in the context of a class rather than an object. In the following example, the foo method does not use any instance references, and can therefore be declared as static .
If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.
PyCharm "thinks" that you might have wanted to have a static method, but you forgot to declare it to be static (using the @staticmethod
decorator).
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 passing a class instance or without even having created a class instance.
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 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.
I can imagine following advantages of having a class method defined as static one:
remaining advantages are probably marginal if present at all:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With