🇨🇳🐲🎉 本网站的中文版本尚处早期开发阶段。如果您发现其中存在错字、纰漏或其他任何问题，请随时联系 [email protected] 向我反馈。

单例在 Python 中的实现

Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.

Singleton has almost the same pros and cons as global variables. Although they're super-handy, they break the modularity of your code.

You can just use a class, which depends on Singleton, in some other context. You'll have to carry the Singleton class as well. Most of the time, this limitation comes up during the creation of unit tests.

Learn more about Singleton

在 Python 中使用模式

Complexity: Low

Popularity: Average

Usage examples: A lot of developers consider the Singleton pattern an antipattern. That's why its usage is on the decline in Python code.

Identification: Singleton can be recognized by a static creation method, which returns the same cached object.