gpt4 book ai didi

python - 如何处理 pylint 消息 : Warning: Method could be a function

转载 作者:IT老高 更新时间:2023-10-28 20:25:03 24 4
gpt4 key购买 nike

我有一个 python 类并针对它运行 pylint。它给出的一条信息是:

Warning: Method could be a function

这是否告诉我最好将此方法移出类,因为它不使用任何实例变量?

在 C# 中,我会将其设为静态方法。这里最 python 的事情是什么?

最佳答案

将它移到一个函数是很常见的,如果它根本不涉及类的话。

如果它操作类属性,使用 classmethod 装饰器:

@classmethod
def spam(cls, ...):
# cls is the class, you can use it to get class attributes
引入了

classmethodstaticmethod(与前者相同,只是该方法不从其第一个参数中获取对该类的引用)最近。这意味着一些 Python 程序员习惯于避免使用静态和类方法。

一些铁杆 Python 程序员会告诉你,这些装饰器只会使事情复杂化;其他一些人(通常是以前的 C# 或 Java 程序员)会告诉您使用函数还不够面向对象。我认为这只是一个偏好问题。

关于python - 如何处理 pylint 消息 : Warning: Method could be a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2674035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com