# The singleton pattern : Always use the same instance ( object ) """ Application scenarios 1、 When creating resources to be reused in the course , Use the same instance , Avoid creating too many objects that waste time and resources """ # Example 1、 Demo singleton mode class Foo: __bar = None @classmethod def bar(cls): if cls.__bar: return cls.__bar else: cls.__bar = Foo() # In fact, the instantiation here return cls.bar() """ 1、 Do not use in singleton mode obj = class() Create objects this way ( In fact, the object is cls.__bar = Foo() Created here ) 2、 The following code is just calling an object method , No matter how many calls are made, the same object ( example ) Methods , So the result is the same """ foo1 = Foo.bar() print(foo1) foo2 = Foo.bar() print(foo2) foo3 = Foo.bar() print(foo3)