Python:如果可能的话,使私有(private)类方法始终静态?

标签 python private static-methods

我知道在其他语言中( C#JAVA 、 ...)制作 "private" 是一种很好的风格。方法(使用两个下划线)static如果可能的话。 有Python的推荐吗?

我宁愿不让它们静态,因为它们只是私有(private)的(我不想将它们移动到实用程序类,并且它们与性能无关),并且我想避免装饰器使我的代码困惑。另一方面,静态代码分析器告诉我这样做(并且我想以“Pythonic”方式编写代码)。

最佳答案

简短的回答是你几乎不应该在 Python 中使用静态方法。他们是added by mistake .

I meant to implement class methods but at first I didn't understand them and accidentally implemented static methods first. - Guido van Rossum

您几乎肯定可以使用类方法或实例方法来实现您想要的目的。

更多关于为什么 Python 中的静态方法大多无用的信息 here .

关于Python:如果可能的话,使私有(private)类方法始终静态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36980441/

相关文章:

python - Tornado : support multiple Application on same IOLoop

python - Python 是否需要深入了解继承链中的所有类?

api - Tumblr v2 API 访问私有(private)博客

C#:从基类静态方法确定派生对象类型

java - 如何从android中的静态方法调用非静态方法

Python套接字等待

python 查找特定元素位置并返回其前面的所有不同元素

python - 在 PIL 中保存 GIF 时透明度不一致

c++ - 如何在 OpenMP 中将对象或结构定义为 threadprivate?

java - 如何创建一个存储用户输入的java方法?