{"id":736,"date":"2021-02-02T20:43:33","date_gmt":"2021-02-02T12:43:33","guid":{"rendered":"https:\/\/kanghaov.com\/?p=736"},"modified":"2021-02-02T20:43:33","modified_gmt":"2021-02-02T12:43:33","slug":"%e8%bd%ac%e8%bd%bdpython%e4%b8%ad%e7%9a%84%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f%e7%9a%84%e5%87%a0%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f%e7%9a%84%e5%8f%8a%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/nemo.cool\/736.html","title":{"rendered":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316"},"content":{"rendered":"<p>AGV\u8c03\u5ea6\u7cfb\u7edf\u4e2d\uff0c\u8d1f\u8d23MQTT\u901a\u8baf\u7684\u6a21\u5757\u9700\u8981\u4e3a\u5355\u4f8b\uff0c\u5728\u535a\u5ba2\u56ed\u627e\u5230\u4e00\u7bc7\u4e0d\u9519\u7684\u6587\u7ae0<\/p>\n<h3>\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<p>\u5355\u4f8b\u6a21\u5f0f\uff08Singleton Pattern\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u786e\u4fdd\u67d0\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\u3002\u5f53\u4f60\u5e0c\u671b\u5728\u6574\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e2a\u7c7b\u53ea\u80fd\u51fa\u73b0\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u5355\u4f8b\u5bf9\u8c61\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u67d0\u4e2a\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u4e00\u4e2a AppConfig \u7684\u7c7b\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u6709\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u521b\u5efa AppConfig \u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u5b58\u5728\u591a\u4e2a AppConfig \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u800c\u8fd9\u6837\u4f1a\u4e25\u91cd\u6d6a\u8d39\u5185\u5b58\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7c7b\u4f3c AppConfig \u8fd9\u6837\u7684\u7c7b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002<\/p>\n<p>\u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f<\/p>\n<h3>\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u65b9\u5f0f<\/h3>\n<h4>1.\u4f7f\u7528\u6a21\u5757<\/h4>\n<p>\u5176\u5b9e\uff0cPython \u7684\u6a21\u5757\u5c31\u662f\u5929\u7136\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u56e0\u4e3a\u6a21\u5757\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\uff0c\u4f1a\u751f\u6210 .pyc \u6587\u4ef6\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5bfc\u5165\u65f6\uff0c\u5c31\u4f1a\u76f4\u63a5\u52a0\u8f7d .pyc \u6587\u4ef6\uff0c\u800c\u4e0d\u4f1a\u518d\u6b21\u6267\u884c\u6a21\u5757\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u628a\u76f8\u5173\u7684\u51fd\u6570\u548c\u6570\u636e\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u4e86\u3002\u5982\u679c\u6211\u4eec\u771f\u7684\u60f3\u8981\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">mysingleton.py\n\nclass Singleton(object):\n    def foo(self):\n        pass\nsingleton = Singleton()\n<\/code><\/pre>\n<p>\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728\u6587\u4ef6 mysingleton.py \u4e2d\uff0c\u8981\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u6b64\u6587\u4ef6\u4e2d\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5373\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u5bf9\u8c61<\/p>\n<pre><code class=\"language-python line-numbers\">from a import singleton\n<\/code><\/pre>\n<h4>2.\u4f7f\u7528\u88c5\u9970\u5668<\/h4>\n<pre><code class=\"language-python line-numbers\">def Singleton(cls):\n    _instance = {}\n\n    def _singleton(*args, **kargs):\n        if cls not in _instance:\n            _instance[cls] = cls(*args, **kargs)\n        return _instance[cls]\n\n    return _singleton\n\n\n@Singleton\nclass A(object):\n    a = 1\n\n    def __init__(self, x=0):\n        self.x = x\n\n\na1 = A(2)\na2 = A(3)\n<\/code><\/pre>\n<h4>3.\u4f7f\u7528\u7c7b<\/h4>\n<pre><code class=\"language-python line-numbers\">class Singleton(object):\n\n    def __init__(self):\n        pass\n\n    @classmethod\n    def instance(cls, *args, **kwargs):\n        if not hasattr(Singleton, \"_instance\"):\n            Singleton._instance = Singleton(*args, **kwargs)\n        return Singleton._instance\n\n<\/code><\/pre>\n<p>\u4e00\u822c\u60c5\u51b5\uff0c\u5927\u5bb6\u4ee5\u4e3a\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u5355\u4f8b\u6a21\u5f0f\uff0c\u4f46\u662f\u8fd9\u6837\u5f53\u4f7f\u7528\u591a\u7ebf\u7a0b\u65f6\u4f1a\u5b58\u5728\u95ee\u9898<\/p>\n<pre><code class=\"language-python line-numbers\">class Singleton(object):\n\n    def __init__(self):\n        pass\n\n    @classmethod\n    def instance(cls, *args, **kwargs):\n        if not hasattr(Singleton, \"_instance\"):\n            Singleton._instance = Singleton(*args, **kwargs)\n        return Singleton._instance\n\nimport threading\n\ndef task(arg):\n    obj = Singleton.instance()\n    print(obj)\n\nfor i in range(10):\n    t = threading.Thread(target=task,args=[i,])\n    t.start()\n\n<\/code><\/pre>\n<p>\u7a0b\u5e8f\u6267\u884c\u540e\uff0c\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n&lt;__main__.Singleton object at 0x02C933D0&gt;\n<\/code><\/pre>\n<p>\u770b\u8d77\u6765\u4e5f\u6ca1\u6709\u95ee\u9898\uff0c\u90a3\u662f\u56e0\u4e3a\u6267\u884c\u901f\u5ea6\u8fc7\u5feb\uff0c\u5982\u679c\u5728init\u65b9\u6cd5\u4e2d\u6709\u4e00\u4e9bIO\u64cd\u4f5c\uff0c\u5c31\u4f1a\u53d1\u73b0\u95ee\u9898\u4e86\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7time.sleep\u6a21\u62df<\/p>\n<p>\u6211\u4eec\u5728\u4e0a\u9762<strong>init<\/strong>\u65b9\u6cd5\u4e2d\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">    def __init__(self):\n        import time\n        time.sleep(1)\n<\/code><\/pre>\n<p>\u91cd\u65b0\u6267\u884c\u7a0b\u5e8f\u540e\uff0c\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre><code class=\"language-python line-numbers\">&lt;__main__.Singleton object at 0x034A3410&gt;\n&lt;__main__.Singleton object at 0x034BB990&gt;\n&lt;__main__.Singleton object at 0x034BB910&gt;\n&lt;__main__.Singleton object at 0x034ADED0&gt;\n&lt;__main__.Singleton object at 0x034E6BD0&gt;\n&lt;__main__.Singleton object at 0x034E6C10&gt;\n&lt;__main__.Singleton object at 0x034E6B90&gt;\n&lt;__main__.Singleton object at 0x034BBA30&gt;\n&lt;__main__.Singleton object at 0x034F6B90&gt;\n&lt;__main__.Singleton object at 0x034E6A90&gt;\n<\/code><\/pre>\n<p>\u95ee\u9898\u51fa\u73b0\u4e86\uff01\u6309\u7167\u4ee5\u4e0a\u65b9\u5f0f\u521b\u5efa\u7684\u5355\u4f8b\uff0c\u65e0\u6cd5\u652f\u6301\u591a\u7ebf\u7a0b<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a\u52a0\u9501\uff01\u672a\u52a0\u9501\u90e8\u5206\u5e76\u53d1\u6267\u884c,\u52a0\u9501\u90e8\u5206\u4e32\u884c\u6267\u884c,\u901f\u5ea6\u964d\u4f4e,\u4f46\u662f\u4fdd\u8bc1\u4e86\u6570\u636e\u5b89\u5168<\/p>\n<pre><code class=\"language-python line-numbers\">import time\nimport threading\nclass Singleton(object):\n    _instance_lock = threading.Lock()\n\n    def __init__(self):\n        time.sleep(1)\n\n    @classmethod\n    def instance(cls, *args, **kwargs):\n        with Singleton._instance_lock:\n            if not hasattr(Singleton, \"_instance\"):\n                Singleton._instance = Singleton(*args, **kwargs)\n        return Singleton._instance\n\n\ndef task(arg):\n    obj = Singleton.instance()\n    print(obj)\nfor i in range(10):\n    t = threading.Thread(target=task,args=[i,])\n    t.start()\ntime.sleep(20)\nobj = Singleton.instance()\nprint(obj)\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n&lt;__main__.Singleton object at 0x02D6B110&gt;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u5dee\u4e0d\u591a\u4e86\uff0c\u4f46\u662f\u8fd8\u662f\u6709\u4e00\u70b9\u5c0f\u95ee\u9898\uff0c\u5c31\u662f\u5f53\u7a0b\u5e8f\u6267\u884c\u65f6\uff0c\u6267\u884c\u4e86time.sleep(20)\u540e\uff0c\u4e0b\u9762\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\uff0c\u6b64\u65f6\u5df2\u7ecf\u662f\u5355\u4f8b\u6a21\u5f0f\u4e86\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u52a0\u4e86\u9501\uff0c\u8fd9\u6837\u4e0d\u592a\u597d\uff0c\u518d\u8fdb\u884c\u4e00\u4e9b\u4f18\u5316\uff0c\u628aintance\u65b9\u6cd5\uff0c\u6539\u6210\u4e0b\u9762\u7684\u8fd9\u6837\u5c31\u884c\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">    @classmethod\n    def instance(cls, *args, **kwargs):\n        if not hasattr(Singleton, \"_instance\"):\n            with Singleton._instance_lock:\n                if not hasattr(Singleton, \"_instance\"):\n                    Singleton._instance = Singleton(*args, **kwargs)\n        return Singleton._instance\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4e00\u4e2a\u53ef\u4ee5\u652f\u6301\u591a\u7ebf\u7a0b\u7684\u5355\u4f8b\u6a21\u5f0f\u5c31\u5b8c\u6210\u4e86<\/p>\n<pre><code class=\"language-python line-numbers\">import time\nimport threading\nclass Singleton(object):\n    _instance_lock = threading.Lock()\n\n    def __init__(self):\n        time.sleep(1)\n\n    @classmethod\n    def instance(cls, *args, **kwargs):\n        if not hasattr(Singleton, \"_instance\"):\n            with Singleton._instance_lock:\n                if not hasattr(Singleton, \"_instance\"):\n                    Singleton._instance = Singleton(*args, **kwargs)\n        return Singleton._instance\n\n\ndef task(arg):\n    obj = Singleton.instance()\n    print(obj)\nfor i in range(10):\n    t = threading.Thread(target=task,args=[i,])\n    t.start()\ntime.sleep(20)\nobj = Singleton.instance()\nprint(obj)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u4f7f\u7528\u65f6\u4f1a\u6709\u9650\u5236\uff0c\u4ee5\u540e\u5b9e\u4f8b\u5316\u5fc5\u987b\u901a\u8fc7 <code>obj = Singleton.instance()<\/code><\/p>\n<p>\u5982\u679c\u7528 <code>obj=Singleton()<\/code> ,\u8fd9\u79cd\u65b9\u5f0f\u5f97\u5230\u7684\u4e0d\u662f\u5355\u4f8b<\/p>\n<h4>4.\u57fa\u4e8e<strong>new<\/strong>\u65b9\u6cd5\u5b9e\u73b0\uff08\u63a8\u8350\u4f7f\u7528\uff0c\u65b9\u4fbf\uff09<\/h4>\n<p>\u901a\u8fc7\u4e0a\u9762\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\uff0c\u5f53\u6211\u4eec\u5b9e\u73b0\u5355\u4f8b\u65f6\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u9700\u8981\u5728\u5185\u90e8\u52a0\u5165\u9501<\/p>\n<p>\u6211\u4eec\u77e5\u9053\uff0c\u5f53\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u662f\u5148\u6267\u884c\u4e86\u7c7b\u7684<strong>new<\/strong>\u65b9\u6cd5\uff08\u6211\u4eec\u6ca1\u5199\u65f6\uff0c\u9ed8\u8ba4\u8c03\u7528object.<strong>new<\/strong>\uff09\uff0c\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff1b\u7136\u540e\u518d\u6267\u884c\u7c7b\u7684<strong>init<\/strong>\u65b9\u6cd5\uff0c\u5bf9\u8fd9\u4e2a\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u6240\u6709\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u8fd9\u4e2a\uff0c\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f<\/p>\n<pre><code class=\"language-python line-numbers\">import threading\nclass Singleton(object):\n    _instance_lock = threading.Lock()\n\n    def __init__(self):\n        pass\n\n\n    def __new__(cls, *args, **kwargs):\n        if not hasattr(Singleton, \"_instance\"):\n            with Singleton._instance_lock:\n                if not hasattr(Singleton, \"_instance\"):\n                    Singleton._instance = object.__new__(cls)  \n        return Singleton._instance\n\nobj1 = Singleton()\nobj2 = Singleton()\nprint(obj1,obj2)\n\ndef task(arg):\n    obj = Singleton()\n    print(obj)\n\nfor i in range(10):\n    t = threading.Thread(target=task,args=[i,])\n    t.start()\n\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">&lt;__main__.Singleton object at 0x038B33D0&gt; &lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n&lt;__main__.Singleton object at 0x038B33D0&gt;\n<\/code><\/pre>\n<p>\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u4ee5\u540e\u5b9e\u4f8b\u5316\u5bf9\u8c61\u65f6\uff0c\u548c\u5e73\u65f6\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e00\u6837<code>obj = Singleton()<\/code><\/p>\n<h4>5.\u57fa\u4e8emetaclass\u65b9\u5f0f\u5b9e\u73b0<\/h4>\n<p>\u76f8\u5173\u77e5\u8bc6<\/p>\n<pre><code class=\"line-numbers\">1.\u7c7b\u7531type\u521b\u5efa\uff0c\u521b\u5efa\u7c7b\u65f6\uff0ctype\u7684__init__\u65b9\u6cd5\u81ea\u52a8\u6267\u884c\uff0c\u7c7b() \u6267\u884ctype\u7684 __call__\u65b9\u6cd5(\u7c7b\u7684__new__\u65b9\u6cd5,\u7c7b\u7684__init__\u65b9\u6cd5)\n2.\u5bf9\u8c61\u7531\u7c7b\u521b\u5efa\uff0c\u521b\u5efa\u5bf9\u8c61\u65f6\uff0c\u7c7b\u7684__init__\u65b9\u6cd5\u81ea\u52a8\u6267\u884c\uff0c\u5bf9\u8c61()\u6267\u884c\u7c7b\u7684 __call__ \u65b9\u6cd5\n<\/code><\/pre>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python line-numbers\">class Foo:\n    def __init__(self):\n        pass\n\n    def __call__(self, *args, **kwargs):\n        pass\n\nobj = Foo()\n# \u6267\u884ctype\u7684 __call__ \u65b9\u6cd5\uff0c\u8c03\u7528 Foo\u7c7b\uff08\u662ftype\u7684\u5bf9\u8c61\uff09\u7684 __new__\u65b9\u6cd5\uff0c\u7528\u4e8e\u521b\u5efa\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528 Foo\u7c7b\uff08\u662ftype\u7684\u5bf9\u8c61\uff09\u7684 __init__\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u5bf9\u8c61\u521d\u59cb\u5316\u3002\n\nobj()    # \u6267\u884cFoo\u7684 __call__ \u65b9\u6cd5    \n<\/code><\/pre>\n<p>\u5143\u7c7b\u7684\u4f7f\u7528<\/p>\n<pre><code class=\"language-python line-numbers\">class SingletonType(type):\n    def __init__(self,*args,**kwargs):\n        super(SingletonType,self).__init__(*args,**kwargs)\n\n    def __call__(cls, *args, **kwargs): # \u8fd9\u91cc\u7684cls\uff0c\u5373Foo\u7c7b\n        print('cls',cls)\n        obj = cls.__new__(cls,*args, **kwargs)\n        cls.__init__(obj,*args, **kwargs) # Foo.__init__(obj)\n        return obj\n\nclass Foo(metaclass=SingletonType): # \u6307\u5b9a\u521b\u5efaFoo\u7684type\u4e3aSingletonType\n    def __init__(self\uff0cname):\n        self.name = name\n    def __new__(cls, *args, **kwargs):\n        return object.__new__(cls)\n\nobj = Foo('xx')\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f<\/p>\n<pre><code class=\"language-python line-numbers\">import threading\n\nclass SingletonType(type):\n    _instance_lock = threading.Lock()\n    def __call__(cls, *args, **kwargs):\n        if not hasattr(cls, \"_instance\"):\n            with SingletonType._instance_lock:\n                if not hasattr(cls, \"_instance\"):\n                    cls._instance = super(SingletonType,cls).__call__(*args, **kwargs)\n        return cls._instance\n\nclass Foo(metaclass=SingletonType):\n    def __init__(self,name):\n        self.name = name\n\n\nobj1 = Foo('name')\nobj2 = Foo('name')\nprint(obj1,obj2)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AGV\u8c03\u5ea6\u7cfb\u7edf\u4e2d\uff0c\u8d1f\u8d23MQTT\u901a\u8baf\u7684\u6a21\u5757\u9700\u8981\u4e3a\u5355\u4f8b\uff0c\u5728\u535a\u5ba2\u56ed\u627e\u5230\u4e00\u7bc7\u4e0d\u9519\u7684\u6587\u7ae0 \u5355\u4f8b\u6a21\u5f0f \u5355\u4f8b\u6a21\u5f0f\uff08Singleton Pattern\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u786e\u4fdd\u67d0\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\u3002\u5f53\u4f60\u5e0c\u671b\u5728\u6574\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e2a\u7c7b\u53ea\u80fd\u51fa\u73b0\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u5355\u4f8b\u5bf9\u8c61\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u3002 \u6bd4\u5982\uff0c\u67d0\u4e2a\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u4e00\u4e2a AppConfig \u7684\u7c7b\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u6709\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u521b\u5efa AppConfig \u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u5b58\u5728\u591a\u4e2a AppConfig \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u800c\u8fd9\u6837\u4f1a\u4e25\u91cd\u6d6a\u8d39\u5185\u5b58\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7c7b\u4f3c AppConfig \u8fd9\u6837\u7684\u7c7b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002 \u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f \u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u65b9\u5f0f 1.\u4f7f\u7528\u6a21\u5757 \u5176\u5b9e\uff0cPython \u7684\u6a21\u5757\u5c31\u662f\u5929\u7136\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u56e0\u4e3a\u6a21\u5757\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\uff0c\u4f1a\u751f\u6210 .pyc \u6587\u4ef6\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5bfc\u5165\u65f6\uff0c\u5c31\u4f1a\u76f4\u63a5\u52a0\u8f7d .pyc \u6587\u4ef6\uff0c\u800c\u4e0d\u4f1a\u518d\u6b21\u6267\u884c\u6a21\u5757\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u628a\u76f8\u5173\u7684\u51fd\u6570\u548c\u6570\u636e\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u4e86\u3002\u5982\u679c\u6211\u4eec\u771f\u7684\u60f3\u8981\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u505a\uff1a mysingleton.py class Singleton(object): def foo(self): pass singleton = Singleton() \u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728\u6587\u4ef6 mysingleton.py \u4e2d\uff0c\u8981\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u6b64\u6587\u4ef6\u4e2d\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5373\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u5bf9\u8c61 from a import singleton 2.\u4f7f\u7528\u88c5\u9970\u5668 def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":737,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[86,2],"tags":[10,92],"class_list":["post-736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-py","tag-python","tag-92"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nemo.cool\/736.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo\" \/>\n<meta property=\"og:description\" content=\"AGV\u8c03\u5ea6\u7cfb\u7edf\u4e2d\uff0c\u8d1f\u8d23MQTT\u901a\u8baf\u7684\u6a21\u5757\u9700\u8981\u4e3a\u5355\u4f8b\uff0c\u5728\u535a\u5ba2\u56ed\u627e\u5230\u4e00\u7bc7\u4e0d\u9519\u7684\u6587\u7ae0 \u5355\u4f8b\u6a21\u5f0f \u5355\u4f8b\u6a21\u5f0f\uff08Singleton Pattern\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u786e\u4fdd\u67d0\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\u3002\u5f53\u4f60\u5e0c\u671b\u5728\u6574\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e2a\u7c7b\u53ea\u80fd\u51fa\u73b0\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u5355\u4f8b\u5bf9\u8c61\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u3002 \u6bd4\u5982\uff0c\u67d0\u4e2a\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u4e00\u4e2a AppConfig \u7684\u7c7b\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u6709\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u521b\u5efa AppConfig \u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u5b58\u5728\u591a\u4e2a AppConfig \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u800c\u8fd9\u6837\u4f1a\u4e25\u91cd\u6d6a\u8d39\u5185\u5b58\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7c7b\u4f3c AppConfig \u8fd9\u6837\u7684\u7c7b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002 \u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f \u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u65b9\u5f0f 1.\u4f7f\u7528\u6a21\u5757 \u5176\u5b9e\uff0cPython \u7684\u6a21\u5757\u5c31\u662f\u5929\u7136\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u56e0\u4e3a\u6a21\u5757\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\uff0c\u4f1a\u751f\u6210 .pyc \u6587\u4ef6\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5bfc\u5165\u65f6\uff0c\u5c31\u4f1a\u76f4\u63a5\u52a0\u8f7d .pyc \u6587\u4ef6\uff0c\u800c\u4e0d\u4f1a\u518d\u6b21\u6267\u884c\u6a21\u5757\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u628a\u76f8\u5173\u7684\u51fd\u6570\u548c\u6570\u636e\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u4e86\u3002\u5982\u679c\u6211\u4eec\u771f\u7684\u60f3\u8981\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u505a\uff1a mysingleton.py class Singleton(object): def foo(self): pass singleton = Singleton() \u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728\u6587\u4ef6 mysingleton.py \u4e2d\uff0c\u8981\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u6b64\u6587\u4ef6\u4e2d\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5373\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u5bf9\u8c61 from a import singleton 2.\u4f7f\u7528\u88c5\u9970\u5668 def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nemo.cool\/736.html\" \/>\n<meta property=\"og:site_name\" content=\"Nemo\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-02T12:43:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nemo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nemo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html\"},\"author\":{\"name\":\"Nemo\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"headline\":\"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316\",\"datePublished\":\"2021-02-02T12:43:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html\"},\"wordCount\":29,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg\",\"keywords\":[\"python\",\"\u5355\u4f8b\u6a21\u5f0f\"],\"articleSection\":[\"Dev\",\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/736.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html\",\"url\":\"https:\\\/\\\/nemo.cool\\\/736.html\",\"name\":\"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg\",\"datePublished\":\"2021-02-02T12:43:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nemo.cool\\\/736.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#primaryimage\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg\",\"width\":2400,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/736.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/nemo.cool\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/#website\",\"url\":\"https:\\\/\\\/nemo.cool\\\/\",\"name\":\"Nemo\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nemo.cool\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/nemo.cool\\\/#\\\/schema\\\/person\\\/698f803ee811e2b140a90f5d5de913d2\",\"name\":\"Nemo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"url\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\",\"caption\":\"Nemo\"},\"logo\":{\"@id\":\"https:\\\/\\\/nemo.cool\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg\"},\"sameAs\":[\"https:\\\/\\\/nemo.cool\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nemo.cool\/736.html","og_locale":"en_US","og_type":"article","og_title":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo","og_description":"AGV\u8c03\u5ea6\u7cfb\u7edf\u4e2d\uff0c\u8d1f\u8d23MQTT\u901a\u8baf\u7684\u6a21\u5757\u9700\u8981\u4e3a\u5355\u4f8b\uff0c\u5728\u535a\u5ba2\u56ed\u627e\u5230\u4e00\u7bc7\u4e0d\u9519\u7684\u6587\u7ae0 \u5355\u4f8b\u6a21\u5f0f \u5355\u4f8b\u6a21\u5f0f\uff08Singleton Pattern\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u7684\u4e3b\u8981\u76ee\u7684\u662f\u786e\u4fdd\u67d0\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\u3002\u5f53\u4f60\u5e0c\u671b\u5728\u6574\u4e2a\u7cfb\u7edf\u4e2d\uff0c\u67d0\u4e2a\u7c7b\u53ea\u80fd\u51fa\u73b0\u4e00\u4e2a\u5b9e\u4f8b\u65f6\uff0c\u5355\u4f8b\u5bf9\u8c61\u5c31\u80fd\u6d3e\u4e0a\u7528\u573a\u3002 \u6bd4\u5982\uff0c\u67d0\u4e2a\u670d\u52a1\u5668\u7a0b\u5e8f\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u653e\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u4e00\u4e2a AppConfig \u7684\u7c7b\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u5982\u679c\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u6709\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f88\u591a\u5730\u65b9\u90fd\u9700\u8981\u521b\u5efa AppConfig \u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u8fd9\u5c31\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u5b58\u5728\u591a\u4e2a AppConfig \u7684\u5b9e\u4f8b\u5bf9\u8c61\uff0c\u800c\u8fd9\u6837\u4f1a\u4e25\u91cd\u6d6a\u8d39\u5185\u5b58\u8d44\u6e90\uff0c\u5c24\u5176\u662f\u5728\u914d\u7f6e\u6587\u4ef6\u5185\u5bb9\u5f88\u591a\u7684\u60c5\u51b5\u4e0b\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7c7b\u4f3c AppConfig \u8fd9\u6837\u7684\u7c7b\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u53ea\u5b58\u5728\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u3002 \u5728 Python \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f \u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u65b9\u5f0f 1.\u4f7f\u7528\u6a21\u5757 \u5176\u5b9e\uff0cPython \u7684\u6a21\u5757\u5c31\u662f\u5929\u7136\u7684\u5355\u4f8b\u6a21\u5f0f\uff0c\u56e0\u4e3a\u6a21\u5757\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\uff0c\u4f1a\u751f\u6210 .pyc \u6587\u4ef6\uff0c\u5f53\u7b2c\u4e8c\u6b21\u5bfc\u5165\u65f6\uff0c\u5c31\u4f1a\u76f4\u63a5\u52a0\u8f7d .pyc \u6587\u4ef6\uff0c\u800c\u4e0d\u4f1a\u518d\u6b21\u6267\u884c\u6a21\u5757\u4ee3\u7801\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u628a\u76f8\u5173\u7684\u51fd\u6570\u548c\u6570\u636e\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u5c31\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u4e86\u3002\u5982\u679c\u6211\u4eec\u771f\u7684\u60f3\u8981\u4e00\u4e2a\u5355\u4f8b\u7c7b\uff0c\u53ef\u4ee5\u8003\u8651\u8fd9\u6837\u505a\uff1a mysingleton.py class Singleton(object): def foo(self): pass singleton = Singleton() \u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u4fdd\u5b58\u5728\u6587\u4ef6 mysingleton.py \u4e2d\uff0c\u8981\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u6b64\u6587\u4ef6\u4e2d\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u5373\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u5bf9\u8c61 from a import singleton 2.\u4f7f\u7528\u88c5\u9970\u5668 def Singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] [&hellip;]","og_url":"https:\/\/nemo.cool\/736.html","og_site_name":"Nemo","article_published_time":"2021-02-02T12:43:33+00:00","og_image":[{"width":2400,"height":800,"url":"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg","type":"image\/jpeg"}],"author":"Nemo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nemo","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nemo.cool\/736.html#article","isPartOf":{"@id":"https:\/\/nemo.cool\/736.html"},"author":{"name":"Nemo","@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"headline":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316","datePublished":"2021-02-02T12:43:33+00:00","mainEntityOfPage":{"@id":"https:\/\/nemo.cool\/736.html"},"wordCount":29,"commentCount":0,"publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"image":{"@id":"https:\/\/nemo.cool\/736.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg","keywords":["python","\u5355\u4f8b\u6a21\u5f0f"],"articleSection":["Dev","Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nemo.cool\/736.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nemo.cool\/736.html","url":"https:\/\/nemo.cool\/736.html","name":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316 - Nemo","isPartOf":{"@id":"https:\/\/nemo.cool\/#website"},"primaryImageOfPage":{"@id":"https:\/\/nemo.cool\/736.html#primaryimage"},"image":{"@id":"https:\/\/nemo.cool\/736.html#primaryimage"},"thumbnailUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg","datePublished":"2021-02-02T12:43:33+00:00","breadcrumb":{"@id":"https:\/\/nemo.cool\/736.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nemo.cool\/736.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/736.html#primaryimage","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2021\/02\/v2-43bc18352e7c2129a009fd90d3756f10_1440w.jpg","width":2400,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/nemo.cool\/736.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/nemo.cool\/"},{"@type":"ListItem","position":2,"name":"[\u8f6c\u8f7d]Python\u4e2d\u7684\u5355\u4f8b\u6a21\u5f0f\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u53ca\u4f18\u5316"}]},{"@type":"WebSite","@id":"https:\/\/nemo.cool\/#website","url":"https:\/\/nemo.cool\/","name":"Nemo","description":"","publisher":{"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nemo.cool\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/nemo.cool\/#\/schema\/person\/698f803ee811e2b140a90f5d5de913d2","name":"Nemo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","url":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","contentUrl":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg","caption":"Nemo"},"logo":{"@id":"https:\/\/nemo.cool\/wp-content\/uploads\/2024\/01\/Big_Hero_6_Anime_HD_desktop_wallpaper_01_1366x768-e1706020097529-96x96.jpg"},"sameAs":["https:\/\/nemo.cool"]}]}},"_links":{"self":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/comments?post=736"}],"version-history":[{"count":0,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/posts\/736\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media\/737"}],"wp:attachment":[{"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/media?parent=736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/categories?post=736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nemo.cool\/wp-json\/wp\/v2\/tags?post=736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}