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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…