Static classes in Python

2019-02-11 12:05发布

I once read (I think on a page from Microsoft) that it's a good way to use static classes, when you don't NEED two or more instances of a class.

I'm writing a program in Python. Is it a bad style, if I use @classmethod for every method of a class?

5条回答
甜甜的少女心
2楼-- · 2019-02-11 12:24

It's terrible style, unless you actually need to access the class.

A static method [...] does not translate to a Python classmethod. Oh sure, it results in more or less the same effect, but the goal of a classmethod is actually to do something that's usually not even possible [...] (like inheriting a non-default constructor). The idiomatic translation of a [...] static method is usually a module-level function, not a classmethod or staticmethod.

source

查看更多
干净又极端
3楼-- · 2019-02-11 12:25

In my experience creating a class is a very good solution for a number of reasons. One is that you wind up using the class as a 'normal' class (esp. making more than just one instance) more often than you might think. It's also a reasonable style choice to stick with classes for everthing; this can make it easier for others who read/maintain your code, esp if they are very OO - they will be comfortable with classes. As noted in other replies, it's also reasonable to just use 'bare' functions for the implementation. You may wish to start with a class and make it a singleton/Borg pattern (lots of examples if you googlefor these); it gives you the flexibility to (re)use the class to meet other needs. I would recommend against the 'static class' approach as being non-conventional and non-Pythonic, which makes it harder to read and maintain.

查看更多
\"骚年 ilove
4楼-- · 2019-02-11 12:39

You might actually want a singleton class rather than a static class: Making a singleton class in python

查看更多
甜甜的少女心
5楼-- · 2019-02-11 12:42

Generally, usage like this is better done by just using functions in a module, without a class at all.

查看更多
趁早两清
6楼-- · 2019-02-11 12:44

There are a few approaches you might take for this. As others have mentioned, you could just use module-level functions. In this case, the module itself is the namespace that holds them together. Another option, which can be useful if you need to keep track of state, is to define a class with normal methods (taking self), and then define a single global instance of it, and copy its instance methods to the module namespace. This is the approach taken by the standard library "random" module -- take a look at lib/python2.5/random.py in your python directory. At the bottom, it has something like this:

# Create one instance, seeded from current time, and export its methods
# as module-level functions.  [...]
_inst = Random()
seed = _inst.seed
random = _inst.random
uniform = _inst.uniform
...

Or you can take the basic approach you described (though I would recommend using @staticmethod rather than @classmethod in most cases).

查看更多
登录 后发表回答