Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python decorator to implement singleton pattern

Shulou Source: shulou.com Published: 2022-06-01 02:13:46 09月21日 Update

This article mainly shows you "how to use Python decorator to achieve singleton pattern", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use Python decorator to achieve singleton pattern" this article.

Decorator implementation: def warpper (clsObject): def inner (* args, * * kwargs): if not hasattr (clsObject, "ins"): insObject = clsObject (* args, * * kwargs) setattr (clsObject, "ins", insObject) return getattr (clsObject "ins") return inner@warpperclass Singleton: passif _ _ name__ = = "_ _ main__": ins = Singleton () print (id (ins)) ins = Singleton () print (id (ins)) these are all the contents of the article "how to use Python decorator to implement singleton pattern" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Model content article learning help easy to understand more organization knowledge editing industry information information channels channels research Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL Docker Microsoft Shulou Technology