#!/usr/bin/env python3# -*- coding: utf-8 -*-__author__ = 'Windrivder'# -----------------------------------------------------------------------------# module singletonclass Module_Singleton(object):def foo(self):pass# module in the import for the first time, will generate the pyc file, when the second import, will direct load. Pyc file, rather than execution module code again. Therefore, we need to define the relevant functions and data in a module, you can get a singleton.module_singleton = Module_Singleton()# -----------------------------------------------------------------------------# use __new__class New_Singleton(object):_instance = Nonedef __new__(cls, *args, **kw):if not cls._instance:cls._instance = super(New_Singleton, cls).__new__(cls, *args, **kw)return cls._instanceclass MyClass(New_Singleton):a = 1# -----------------------------------------------------------------------------# use decoratorfrom functools import wrapsdef decorator_singleton(cls):instances = {}@wraps(cls)def getinstance(*args, **kw):if cls not in instances:instances[cls] = cls(*args, **kw)return instances[cls]return getinstance@decorator_singletonclass My_Test(object):a = 1# -----------------------------------------------------------------------------# use metaclassclass Metaclass_Singleton(type):_instances = {}def __call__(cls, *args, **kwargs):if cls not in cls._instances:cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)return cls._instances[cls]class My_Test3(metaclass=Metaclass_Singleton):pass# -----------------------------------------------------------------------------# 实例的唯一性并不是重要的,我们应该关注的是实例的状态,只要所有的实例共享状态,行为一致,那就达到了单例的目的class Borg:_shared_state = {}def __init__(self):self.__dict__ = self._shared_stateif __name__ == '__main__':one = MyClass()two = MyClass()print(one==two)print(one is two)print(id(one), id(two))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。