Why does pycharm propose to change method to static

前端 未结 9 1572
面向向阳花
面向向阳花 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条回答
  •  Happy的楠姐
    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!

提交回复
热议问题