"init"输出三次
class ConfigLoader(object):
_instance = None
_lock = threading.Lock()
def __new__(cls, *args, **kw):
if cls._instance is None:
with cls._lock:
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def __init__(self, config_file_name="config.json"):
print("init")
self.file_path = os.path.join(os.getcwd(), 'config', config_file_name)
self.json_data = None
c1 = ConfigLoader(config_file_name="config.json")
c2 = ConfigLoader()
c3 = ConfigLoader()
object.__new__
构造了三次,
__init__
就会被调用三次。