Home > Article > Backend Development > What are the common ways to implement the singleton pattern in Python?
class SingletonType(type): """ 单例元类。用于将普通类转换为单例类。 """ _instances = {} # 存储单例实例的字典 def __call__(cls, *args, **kwargs): """ 重写 __call__ 方法。用于创建和返回单例实例。 """ if cls not in cls._instances: # 如果类还没有实例化过 cls._instances[cls] = super().__call__(*args, **kwargs) # 则创建新实例并存储在字典中 return cls._instances[cls] # 返回字典中的实例 class MyClass(metaclass=SingletonType): """ 单例类。使用元类 SingletonType 将其转换为单例类。 """ def __init__(self, name): self.name = name def say_hello(self): print(f"Hello, my name is {self.name}.") # 创建 MyClass 的两个实例,应该是同一个对象 obj1 = MyClass("Alice") obj2 = MyClass("Bob") # 打印两个实例的内存地址,应该相同 print(hex(id(obj1))) # 输出:0x7f8d94547a90 print(hex(id(obj2))) # 输出:0x7f8d94547a90 # 调用两个实例的方法,输出应该相同 obj1.say_hello() # 输出:Hello, my name is Alice. obj2.say_hello() # 输出:Hello, my name is Alice.
In the above code, we define a class named SingletonType
's metaclass and use it as the metaclass of MyClass
. In the SingletonType
class, we maintain an _instances
dictionary that stores unique instances of each class. In the __call__()
method, we check the _instances
dictionary and if the class does not already have an instance, create a new instance and add it to _instances
. Finally, we return the instances in _instances
.
In the MyClass
class, we define a constructor with parameters and use the metaclass
parameter to specify the SingletonType
metaclass. Since the MyClass
class uses the SingletonType
metaclass, it has singleton behavior. In the program, we create two instances of MyClass
, obj1
and obj2
, and then print their memory addresses to verify whether they are the same object. Finally, we call the method on both instances and the output should be the same.
def singleton(cls): """ 单例装饰器。用于将普通类转换为单例类。 """ instances = {} # 存储单例实例的字典 def get_instance(*args, **kwargs): """ 获取单例实例的方法。 """ if cls not in instances: # 如果类还没有实例化过 instances[cls] = cls(*args, **kwargs) # 则创建新实例并存储在字典中 return instances[cls] # 返回字典中的实例 return get_instance @singleton class MyClass: """ 单例类。使用装饰器 singleton 将其转换为单例类。 """ def __init__(self, name): self.name = name def say_hello(self): print(f"Hello, my name is {self.name}.") # 创建 MyClass 的两个实例,应该是同一个对象 obj1 = MyClass("Alice") obj2 = MyClass("Bob") # 打印两个实例的内存地址,应该相同 print(hex(id(obj1))) # 输出:0x7f8d94547a90 print(hex(id(obj2))) # 输出:0x7f8d94547
In the above code, we define a decorator function named singleton
. Inside the singleton
function, we create an instances
dictionary to store a unique instance of each class. Then, we define an internal function called get_instance
to get the singleton instance. In the get_instance
function, we check the instances
dictionary and if the class does not already have an instance, create a new instance and add it to instances
. Finally, we return the instance in the dictionary.
Apply the @singleton
decorator on the MyClass
class to convert it into a singleton class. Since this decorator operates on classes, it can easily convert any normal class into a singleton class. In the program, we create two instances of MyClass
, obj1
and obj2
, and then print their memory addresses to verify whether they are the same object. Finally, we call the method on both instances and the output should be the same.
# mymodule.py class MyClass: """ 单例类。 """ def __init__(self, name): self.name = name def say_hello(self): print(f"Hello, my name is {self.name}.") my_singleton = MyClass("Alice") # 创建单例实例
# main.py from mymodule import my_singleton # 使用单例实例 my_singleton.say_hello() # 输出:Hello, my name is Alice.
In the above code, we define the MyClass
class in an independent module mymodule.py
, and created a singleton instance my_singleton
in it. Then, in another file main.py
, we import the my_singleton
instance from the mymodule
module and use it to call say_hello()
method.
Since Python modules are automatically executed when first imported, we can take advantage of this feature to create singleton instances. In the mymodule.py
module we can ensure that my_singleton
is only created once and share it among other parts of the program.
class MyClass: """ 单例类。 """ _instance = None # 存储单例实例的类变量 def __new__(cls, *args, **kwargs): """ 重写 __new__ 方法。用于创建和返回单例实例。 """ if cls._instance is None: # 如果类还没有实例化过 cls._instance = super().__new__(cls) # 则创建新实例并存储在类变量中 return cls._instance # 返回类变量中的实例 def __init__(self, name): self.name = name def say_hello(self): print(f"Hello, my name is {self.name}.") # 创建 MyClass 的两个实例,应该是同一个对象 obj1 = MyClass("Alice") obj2 = MyClass("Bob") # 打印两个实例的内存地址,应该相同 print(hex(id(obj1))) # 输出:0x7f8d94547a90 print(hex(id(obj2))) # 输出:0x7f8d94547a90 # 调用两个实例的方法,输出应该相同 obj1.say_hello() # 输出:Hello, my name is Alice. obj2.say_hello() # 输出:Hello, my name is Alice.
In the above code, we change the constructor of the MyClass
class to the __new__()
method and use _instance
Class variables to store singleton instances. In the __new__()
method, we check the _instance
variable and if the class does not already have an instance, create a new instance and add it to _instance
. Finally, we return the instance in _instance
.
In the program, we create two instances of MyClass
obj1
and obj2
, and then print their memory addresses to verify that they are the same object. Finally, we call the method on both instances and the output should be the same.
No matter which method is used to implement the singleton pattern, you need to pay attention to issues such as thread safety and scalability. Therefore, in actual development, please carefully consider your needs and choose an appropriate implementation.
The above is the detailed content of What are the common ways to implement the singleton pattern in Python?. For more information, please follow other related articles on the PHP Chinese website!