首页 > 编程语言 >python 单例

python 单例

时间:2023-02-12 12:24:15浏览次数:42  
标签:__ args python kwargs instances 单例 ._ cls

 

 

import threading

class ThreadSafeSingleton(type):
  _instances= {}
  _singleton_lock =threading.Lock()

def __call__(cls, *args, **kwargs):
  if cls not in cls._instances:
    with cls._singleton_lock:
      if cls not in cls._instances:
        cls._instances[cls] = super(ThreadSafeSingleton,cls).__call__(*args, **kwargs)
  return cls._instances[cls]

class M2TWConfig(metaclass=ThreadSafeSingleton):

  def __init__(self, *args, **kwargs):

    pass

 

标签:__,args,python,kwargs,instances,单例,._,cls
From: https://www.cnblogs.com/zhangjunrui/p/17113615.html

相关文章