问题描述
我来自Java背景,我是Python的新手.我有几个脚本,可以共享一些与阅读和编写文件有关的应用程序独有的助手函数.一些与阅读相关的功能,有些功能与写作有关.在搜索正确的方法时,我看到了: python中的静态方法?
他在答案中提到:
最后,少量使用静态方法!在Python中,很少有静态方法在很少的情况下,我看到它们使用了很多次,而单独的"顶级"功能会更清晰.
我不太了解顶级功能,我不确定这个简单的示例更好:1)为具有静态读取器功能的读者创建类,对于作者或2)将这些帮助者声明为全球功能,为什么?
编辑:真的关于这个主题的好文章,我刚刚找到 http://tomayko.com/writings/the-static-method-thing
推荐答案
在Java中,使用(以恕我直言)的想法是在任何地方使用类,甚至只将不共享任何状态的静态函数组合在一起(因此,将永远不会实例化).
python在这里有所不同.如果您的功能没有共享状态 1 (因此,在Java中通常是static函数),并且与"真实"类无关(=一个是实际实例化)您只需在模块内使用免费功能.
其背后的原因是只有当您实际想实例化时才需要一个类,因此将类作为几个功能的容器,这些功能不需要共享特定于实例的状态是没有用的.
实际上,您可以将模块视为static类 - 即功能的容器(=静态方法),模块变量(=静态字段)和类型.
Python中的好处是,具有顶级功能不会给出全局名称空间问题,因为在Python中,顶级函数/对象/...仍然是模块分配的.因此,您仍然可以通过模块分组功能,而无需不必要的class -tax.
- 实际上,他们 can 具有一些共享状态,形式是模块级变量(So,singletons);同样,类比模块静态类似乎都成立.
其他推荐答案
来自Python的禅宗(import this):
Namespaces are one honking great idea -- let's do more of those!
用Java这样的语言创建静态方法的主要原因之一是确保这些方法不会最终污染全局名称空间. (尽管Java完全不使用"软件包级"函数来强制执行自己的命名空间约定!)在Python中,所有"顶级"功能都会自动放置在包含这些功能的模块的名称空间中,因此没有污染全局的危险命名空间.
换句话说,像许多其他语言一样,Python可以以几种不同的方式创建名称空间.在这种情况下,几乎不需要创建一个仅包含静态方法的类,当模块提供相同的命名间隙目的而没有与定义类关联的混乱(或认知负载).
其他推荐答案
如果该函数与类有关,则将其作为静态方法.例如:
class DBobject(): name = StringProperty() @staticmethod def get_by_name(name): return db.select('select * from DBobject where name = "%s"' % name) python = DBobject.get_by_name('python')
也就是说,该方法与dbobject类完全相关,因此应该是静态方法.
问题描述
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.
- actually, they can have some shared state, in form of module-level variables (so, singletons); again, the analogy modules-static classes seems to hold.
其他推荐答案
From the Zen of Python (import this) :
Namespaces are one honking great idea -- let's do more of those!
One of the main reasons to create static methods in a language like Java is to ensure that those methods don't wind up polluting the global namespace. (Although Java enforces its own namespace conventions by disallowing "package-level" functions entirely !) In Python, all "top-level" functions are automatically placed within the namespace of the module containing those functions, so there's no danger of polluting the global namespace this way.
In other words, like many other languages, Python can create namespaces in a few different ways. In this case, there's little need to create a class containing only static methods, when a module serves the same namespacing purpose without the clutter (or cognitive load) associated with defining a class.
其他推荐答案
If the function is related to a class, then make it a static method. For example:
class DBobject(): name = StringProperty() @staticmethod def get_by_name(name): return db.select('select * from DBobject where name = "%s"' % name) python = DBobject.get_by_name('python')
That is, the method is completely related to the DBobject class so it should be a static method.