Python:如果可能,让私有 class 方法始终是静态的?

Python: make private class methods always static if possible?

我知道它在其他语言中的风格很好(C#, JAVA, ...) to make "private" methods (using two underscores) static 如果可能的话。 Python有什么推荐吗?

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

在python中没有'private method'这样的东西,所有的方法都是public。唯一告诉您方法不打算使用 publicly 的是它的名称以下划线开头。

如果您的方法没有引用 self(class 的一个实例)中的任何内容,那么您可以将它们设为静态以优化应用程序的内存使用。

简短的回答是你几乎不应该在 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

您几乎可以肯定地使用 class 方法或实例方法实现您想要的。

详细了解为什么 Python 中的静态方法大多无用 here