Why does pycharm propose to change method to static(为什么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建议将方法更改为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么pycharm建议将方法更改为静态
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
