{"id":1072531,"date":"2025-01-08T11:17:03","date_gmt":"2025-01-08T03:17:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072531.html"},"modified":"2025-01-08T11:17:05","modified_gmt":"2025-01-08T03:17:05","slug":"python%e5%a6%82%e4%bd%95%e5%8f%aa%e6%89%a7%e8%a1%8c%e4%b8%80%e6%ac%a1%e4%bb%a3%e7%a0%81-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072531.html","title":{"rendered":"python\u5982\u4f55\u53ea\u6267\u884c\u4e00\u6b21\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102724\/088c79f3-f570-41e5-a54d-54bfe754c902.webp\" alt=\"python\u5982\u4f55\u53ea\u6267\u884c\u4e00\u6b21\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python \u53ea\u6267\u884c\u4e00\u6b21\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u548c\u5355\u4f8b\u6a21\u5f0f\u3001\u4f7f\u7528\u88c5\u9970\u5668\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u6761\u4ef6\u8bed\u53e5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u662f\u8f83\u4e3a\u5e38\u89c1\u4e14\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5355\u4f8b\u6a21\u5f0f\u5728 Python \u4e2d\u7684\u5b9e\u73b0\uff0c\u4ee5\u53ca\u5176\u4ed6\u51e0\u79cd\u5e38\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u5b83\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def execute_once():<\/p>\n<p>    print(&quot;This code is executed once.&quot;)<\/p>\n<p>execute_once()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>execute_once<\/code>\u51fd\u6570\u4e2d\u7684\u4ee3\u7801\u53ea\u4f1a\u5728\u8c03\u7528\u7684\u65f6\u5019\u6267\u884c\u4e00\u6b21\u3002\u5982\u679c\u4e0d\u518d\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u4ee3\u7801\u4e5f\u4e0d\u4f1a\u518d\u6b21\u6267\u884c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528\u7c7b\u548c\u5355\u4f8b\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002Python \u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff0c\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u7c7b\u53d8\u91cf<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    def __new__(cls, *args, kwargs):<\/p>\n<p>        if not cls._instance:<\/p>\n<p>            cls._instance = super(Singleton, cls).__new__(cls, *args, kwargs)<\/p>\n<p>        return cls._instance<\/p>\n<p>    def __init__(self):<\/p>\n<p>        print(&quot;This code is executed once.&quot;)<\/p>\n<p>singleton = Singleton()<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Singleton<\/code>\u7c7b\u901a\u8fc7\u7c7b\u53d8\u91cf<code>_instance<\/code>\u6765\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u88ab\u521b\u5efa\uff0c\u5e76\u4e14<code>__init__<\/code>\u65b9\u6cd5\u4e2d\u7684\u4ee3\u7801\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u88c5\u9970\u5668<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def singleton(cls):<\/p>\n<p>    instances = {}<\/p>\n<p>    def get_instance(*args, kwargs):<\/p>\n<p>        if cls not in instances:<\/p>\n<p>            instances[cls] = cls(*args, kwargs)<\/p>\n<p>        return instances[cls]<\/p>\n<p>    return get_instance<\/p>\n<p>@singleton<\/p>\n<p>class Singleton:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        print(&quot;This code is executed once.&quot;)<\/p>\n<p>singleton = Singleton()<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668<code>singleton<\/code>\uff0c\u53ef\u4ee5\u5c06\u4efb\u4f55\u7c7b\u8f6c\u6362\u4e3a\u5355\u4f8b\u6a21\u5f0f\u3002\u88c5\u9970\u5668\u901a\u8fc7\u4e00\u4e2a\u5b57\u5178<code>instances<\/code>\u6765\u5b58\u50a8\u7c7b\u7684\u5b9e\u4f8b\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u6761\u4ef6\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u6761\u4ef6\u8bed\u53e5\u4e5f\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">executed = False<\/p>\n<p>def execute_once():<\/p>\n<p>    global executed<\/p>\n<p>    if not executed:<\/p>\n<p>        print(&quot;This code is executed once.&quot;)<\/p>\n<p>        executed = True<\/p>\n<p>execute_once()<\/p>\n<p>execute_once()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u5168\u5c40\u53d8\u91cf<code>executed<\/code>\u6765\u8bb0\u5f55\u4ee3\u7801\u662f\u5426\u5df2\u7ecf\u6267\u884c\u8fc7\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7ebf\u7a0b\u5b89\u5168\u7684\u5355\u4f8b\u6a21\u5f0f<\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u786e\u4fdd\u5355\u4f8b\u6a21\u5f0f\u7684\u7ebf\u7a0b\u5b89\u5168\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    _lock = threading.Lock()<\/p>\n<p>    def __new__(cls, *args, kwargs):<\/p>\n<p>        if not cls._instance:<\/p>\n<p>            with cls._lock:<\/p>\n<p>                if not cls._instance:<\/p>\n<p>                    cls._instance = super(Singleton, cls).__new__(cls, *args, kwargs)<\/p>\n<p>        return cls._instance<\/p>\n<p>    def __init__(self):<\/p>\n<p>        print(&quot;This code is executed once.&quot;)<\/p>\n<p>singleton = Singleton()<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>threading.Lock<\/code>\u6765\u786e\u4fdd\u4ee3\u7801\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4e5f\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u9501\u673a\u5236\u786e\u4fdd\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8fdb\u5165\uff0c\u786e\u4fdd\u5355\u4f8b\u6a21\u5f0f\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf<\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6a21\u5757\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5355\u4f8b\u3002\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u901a\u8fc7\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u6765\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module_singleton.py<\/p>\n<p>if &#39;singleton_instance&#39; not in globals():<\/p>\n<p>    print(&quot;This code is executed once.&quot;)<\/p>\n<p>    singleton_instance = True<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import module_singleton<\/p>\n<p>import module_singleton<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u68c0\u67e5\u6a21\u5757\u7684\u5168\u5c40\u53d8\u91cf<code>singleton_instance<\/code>\u662f\u5426\u5b58\u5728\uff0c\u6765\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\u3002\u7531\u4e8e\u6a21\u5757\u5728\u7b2c\u4e00\u6b21\u5bfc\u5165\u65f6\u4f1a\u6267\u884c\u4e00\u6b21\uff0c\u540e\u7eed\u518d\u6b21\u5bfc\u5165\u65f6\u4e0d\u4f1a\u91cd\u590d\u6267\u884c\uff0c\u56e0\u6b64\u53ef\u4ee5\u8fbe\u5230\u53ea\u6267\u884c\u4e00\u6b21\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u5305\u62ec\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u548c\u5355\u4f8b\u6a21\u5f0f\u3001\u4f7f\u7528\u88c5\u9970\u5668\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u6761\u4ef6\u8bed\u53e5\u4ee5\u53ca\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u786e\u4fdd\u4ee3\u7801\u7684\u6267\u884c\u53ea\u53d1\u751f\u4e00\u6b21\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u6709\u6548\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5f88\u597d\u5730\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u901a\u8fc7\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ece\u800c\u786e\u4fdd\u4ee3\u7801\u7684\u6267\u884c\u903b\u8f91\u548c\u6027\u80fd\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u786e\u4fdd\u67d0\u6bb5\u4ee3\u7801\u53ea\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u6267\u884c\u4e00\u6b21\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u786e\u4fdd\u67d0\u6bb5\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u4ee3\u7801\u6216\u51fd\u6570\u5185\u90e8\u7684\u6807\u5fd7\u53d8\u91cf\u3002\u901a\u8fc7\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u6765\u8ddf\u8e2a\u4ee3\u7801\u662f\u5426\u5df2\u7ecf\u6267\u884c\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u91cd\u590d\u6267\u884c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4ec0\u4e48\u5de5\u5177\u6216\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5b9e\u73b0\u53ea\u6267\u884c\u4e00\u6b21\u7684\u4ee3\u7801\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u4e2d\u7684<code>Lock<\/code>\u5bf9\u8c61\uff0c\u4ee5\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u67d0\u6bb5\u4ee3\u7801\u53ea\u88ab\u6267\u884c\u4e00\u6b21\u3002\u6b64\u5916\uff0c<code>contextlib<\/code>\u6a21\u5757\u4e2d\u7684<code>contextmanager<\/code>\u4e5f\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u53ea\u6267\u884c\u4e00\u6b21\u7684\u4ee3\u7801\u5757\uff0c\u63d0\u4f9b\u4e0a\u4e0b\u6587\u7ba1\u7406\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5728\u7c7b\u4e2d\u5b9e\u73b0\u67d0\u4e2a\u65b9\u6cd5\u53ea\u88ab\u8c03\u7528\u4e00\u6b21\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u5728\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u8bbe\u7f6e\u4e00\u4e2a\u6807\u5fd7\u5c5e\u6027\uff0c\u6307\u793a\u65b9\u6cd5\u662f\u5426\u5df2\u88ab\u8c03\u7528\u3002\u901a\u8fc7\u68c0\u67e5\u8fd9\u4e2a\u6807\u5fd7\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u65b9\u6cd5\u7684\u6267\u884c\uff0c\u786e\u4fdd\u5b83\u53ea\u5728\u7b2c\u4e00\u6b21\u8c03\u7528\u65f6\u8fd0\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u7c7b\u7684\u65b9\u6cd5\u6267\u884c\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u53ea\u6267\u884c\u4e00\u6b21\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u7c7b\u548c\u5355\u4f8b\u6a21\u5f0f\u3001\u4f7f\u7528\u88c5\u9970\u5668\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u548c\u6761\u4ef6\u8bed\u53e5\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1072535,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072531"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1072531"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072531\/revisions"}],"predecessor-version":[{"id":1072538,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072531\/revisions\/1072538"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072535"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}