Why pycharm suggests changing the method to static

The new release of pycharm (community version 3.1.3) suggests converting methods that do not work with the current state of the object into static.

enter image description here

What is the practical reason for this? Some kind of micro-performance (or memory) -optimization?

+64
python pycharm
May 9 '14 at
source share
7 answers

PyCharm "thinks" that you might have wanted a static method, but you forgot to declare it static.

PyCharm suggests this because the method does not use self in its body and, therefore, does not actually modify the class instance. Therefore, a method can be static, that is, called without creating an instance of the class earlier.

+71
May 9 '14 at 1:35
source share

Consistent with @jolvi, @ArundasR and others, the warning occurs in a member function that does not use self .

If you are sure that PyCharm is wrong, the function should not be @staticmethod , and if you set zero warnings, you can do this in one of two ways:

Workaround number 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 to create a handler table to respond to the protocol subtype field. All handlers must be of the same shape (static or non-static). But some failed to do anything with this instance. If I made these static, I would get a "TypeError: staticmethod object" cannot be called. "

In support of the OP's concern, inviting you to add a staticmethod whenever you can goes against the principle that it is easier to make the code less restrictive later than to make it more - creating a static method makes it less strict, since you can call class.f () instead of instance.f ().

Guess why this warning exists:

  • He advertises a staticmethod . This makes developers aware of what they might intend.
  • As @JohnWorrall points out, this gets your attention when I was accidentally left with this feature.
  • It is a river for rethinking the object model; perhaps the function does not belong in this class at all.
+11
Aug 03 '15 at 13:27
source share

I can imagine the following benefits of having a class method defined as static:

  • you can call a method just by using the class name, no need to create it.

The remaining benefits are likely marginal if present at all:

  • may work a little faster
  • save some memory
+6
May 9 '14 at
source share

I think the reason for this warning is configuration in Pycharm. You can deselect. The method can be static in the editor-> Inspection.

+4
Jun 21 '16 at 17:21
source share

This error message just helped me with a bunch, since I did not understand that I accidentally wrote my function using my example of testing a player

 my_player.attributes[item] 

instead of the right way

 self.attributes[item] 
+3
Aug 07 '14 at 8:15
source share

Since you did not reference self in the body of the bar method, PyCharm asks if it is possible that you want to make the bar text static. Other programming languages, such as Java, have obvious reasons for declaring a static method. In Python, the only real advantage of a static method (AFIK) can be called without an instance of the class. However, if this is the only reason, you are probably better off going with the top level function - as noted here .

In short, I'm not one hundred percent sure why he is there. I guess they will probably remove it in an upcoming release.

+3
Sep 11 '14 at 19:06
source share

I agree with the answers given here (the method does not use self and therefore can be decorated with @staticmethod ).

I would like to add that you might want to transfer the method to a top-level function instead of the static method inside the class. See this question and the accepted answer for more details: python - use static methods or top-level functions

Moving a method to a top-level function will also clear the PyCharm warning.

+1
Jun 08 '16 at 14:34
source share



All Articles