为什么pycharm建议将方法更改为静态 [英] Why does pycharm propose to change method to static

查看:37
本文介绍了为什么pycharm建议将方法更改为静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的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建议将方法更改为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆