MCPcopy Create free account
hub / github.com/Jack-Lee-Hiter/AlgorithmsByPython / singleton

Function singleton

Target Offer/Singleton.py:65–71  ·  view source on GitHub ↗
(cls, *args, **kwargs)

Source from the content-addressed store, hash-verified

63单例类本身根本不知道自己是单例的,因为他自己的代码并不是单例的
64'''
65def singleton(cls, *args, **kwargs):
66 instances = {}
67 def getinstance():
68 if cls not in instances:
69 instances[cls] = cls(*args, **kwargs)
70 return instances[cls]
71 return getinstance
72@singleton
73class MyClass3(object):
74 a = 1

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected