python - 应该使用静态方法或顶级函数 [英] python - should I use static methods or top-level functions

查看:358
本文介绍了python - 应该使用静态方法或顶级函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java的背景,我是python的新手。我有几个脚本,共享一些帮助器功能独特的应用程序相关的阅读和写入文件。一些与阅读有关的功能,一些写作。在寻找正确的方法时,我看到: Python中的静态方法



他在答案中提到:


最后,请谨慎使用staticmethod!在Python中有很少的静态方法是必需的,我已经看到它们使用了很多次,单独的顶级功能将会更清晰。


我不太了解顶级功能,我不知道给出这个更好的简单示例:1)为具有静态阅读器功能的读者创建一个类作为一个作家,或者2)将这些帮助者声明为全局功能,为什么?



编辑:真的关于这个主题的好文章我刚刚发现 http://tomayko.com/writings/the-static-method-thing

解决方案

在Java中有(IMHO错误)的想法,即使在任何地方使用类,即使只是将不共享任何状态的静态函数组合在一起(因此这些类永远不会被实例化)。



这里的Python要求不同;如果你有没有一些共享状态的功能 1 (因此在Java中通常是 static 函数)并且不紧密相关到一个真正的类(=实际实例化的一个)你只是在模块中使用免费的功能。



这样做的原因是一个类只有在你实际上想要实例化它,因此有一个类就像几个不需要共享一个实例的状态的函数的容器是无用的。



其实,你可以认为一个模块是一个 static class - 即一个函数容器(= static methods),模块变量(= static fields)和类型。



Python中的好东西是具有顶级功能不会给全局命名空间污染问题,因为在Python顶级函数/ objects /中。 ..仍然是模块范围。因此,您仍然可以按模块分组函数,而不需要不必要的 -tax。







  1. 实际上,他们可以具有一些共享状态,以模块级变量的形式(so,singleton);再次,类比模块 - 静态类似乎成立。


I come from a Java background and I'm new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated with reading, some with writing. While searching for the correct approach, I saw this: Static methods in Python?

He mentions in his answer:

Finally, use staticmethod sparingly! There are very few situations where static-methods are necessary in Python, and I've seen them used many times where a separate "top-level" function would have been clearer.

I don't understand top-level functions very well and I'm not sure given this simple example which is better: 1) create a class for a reader with static reader functions and the same for a writer or 2) to declare these helpers as global functions and why?

EDIT: REALLY good article about this subject i just found http://tomayko.com/writings/the-static-method-thing

解决方案

In Java there's the (IMHO wrong) idea to use classes everywhere, even just group together static functions that don't share any state (and thus such classes will never be instantiated).

Python here begs to differ; if you have functions that don't have some shared state1 (and thus in Java would typically be static functions) and aren't tightly related to a "real" class (=one that is actually instantiated) you just use free functions inside a module.

The reasoning behind this is that a class is needed only when you actually want to instantiate it, thus having a class just as a container for several functions that don't need to share an instance-specific state is useless.

Actually, you can somewhat think of a module as a static class - i.e. a container of functions (=static methods), module variables (=static fields) and types.

The nice thing in Python is that having top-level function doesn't give global namespace pollution problems, since in Python top-level functions/objects/... are still module-scoped. Thus, you can still group functions by module, without the unnecessary class-tax.


  1. actually, they can have some shared state, in form of module-level variables (so, singletons); again, the analogy modules-static classes seems to hold.

这篇关于python - 应该使用静态方法或顶级函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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