Why does pycharm propose to change method to static

前端 未结 9 1594
面向向阳花
面向向阳花 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: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 
        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.

提交回复
热议问题