Why does pycharm propose to change method to static

前端 未结 9 1518
面向向阳花
面向向阳花 2020-12-22 18:41

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.

相关标签:
9条回答
  • 2020-12-22 18:48

    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.

    0 讨论(0)
  • 2020-12-22 18:50

    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:

    • It advertises staticmethod. It makes developers aware of something they may well have intended.
    • As @JohnWorrall's points out, it gets your attention when self was inadvertently left out of the function.
    • It's a cue to rethink the object model; maybe the function does not belong in this class at all.
    0 讨论(0)
  • 2020-12-22 18:51

    I can imagine following advantages of having a class method defined as static one:

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

    remaining advantages are probably marginal if present at all:

    • might run a bit faster
    • save a bit of memory
    0 讨论(0)
  • 2020-12-22 18:52

    It might be a bit messy, but sometimes you just don't need to access self, but you would prefer to keep the method in the class and not make it static. Or you just want to avoid adding a bunch of unsightly decorators. Here are some potential workarounds for that situation.

    If your method only has side effects and you don't care about what it returns:

    def bar(self):
        doing_something_without_self()
        return self
    

    If you do need the return value:

    def bar(self):
        result = doing_something_without_self()
        if self:
            return result
    

    Now your method is using self, and the warning goes away!

    0 讨论(0)
  • 2020-12-22 18:54

    The reason why Pycharm make it as a warning because Python will pass self as the first argument when calling a none static method (not add @staticmethod). Pycharm knows it.

    Example:

    class T:
        def test():
            print "i am a normal method!"
    
    t = T()
    t.test()
    output:
    Traceback (most recent call last):
      File "F:/Workspace/test_script/test.py", line 28, in <module>
        T().test()
    TypeError: test() takes no arguments (1 given)
    

    I'm from Java, in Java "self" is called "this", you don't need write self(or this) as argument in class method. You can just call self as you need inside the method. But Python "has to" pass self as a method argument.

    By understanding this you don't need any Workaround as @BobStein answer.

    0 讨论(0)
  • 2020-12-22 18:56

    I think that the reason for this warning is config in Pycharm. You can uncheck the selection Method may be static in Editor->Inspection

    0 讨论(0)
提交回复
热议问题