问题描述
新的 pycharm 版本(3.1.3 社区版)提议将不适用于当前对象状态的方法转换为静态方法.
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.
这样做的实际原因是什么?某种微性能(或内存)优化?
What is the practical reason for that? Some kind of micro-performance(-or-memory)-optimization?
推荐答案
PyCharm认为"您可能想要有一个静态方法,但您忘记将其声明为静态方法(使用@staticmethod
装饰器).
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 提出这个建议是因为该方法在其主体中没有使用self
,因此实际上没有改变类实例.因此,该方法可以是静态的,即无需传递类实例或什至无需创建类实例即可调用.
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.
这篇关于为什么pycharm建议把method改成static的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!