{"id":966883,"date":"2024-12-27T04:51:46","date_gmt":"2024-12-26T20:51:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/966883.html"},"modified":"2024-12-27T04:51:48","modified_gmt":"2024-12-26T20:51:48","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%85%a8%e5%b1%80%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/966883.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182433\/ad9da9c1-bbd6-44f9-bea9-05f7264607af.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u5168\u5c40\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u5b57\u5178\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u5b57\u5178\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<\/strong>\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u8981\u6839\u636e\u4ee3\u7801\u7684\u7ec4\u7ec7\u7ed3\u6784\u548c\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u5b57\u5178<\/strong>\uff1a\u8fd9\u662f\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u811a\u672c\u7684\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u5b57\u5178\uff0c\u4f7f\u5176\u5728\u6574\u4e2a\u811a\u672c\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\uff0c\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e2d\u8bbf\u95ee\u6216\u4fee\u6539\u4e00\u4e2a\u5b57\u5178\uff0c\u90a3\u4e48\u53ef\u4ee5\u5728\u51fd\u6570\u5916\u90e8\u76f4\u63a5\u58f0\u660e\u8fd9\u4e2a\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5168\u5c40\u5b57\u5178<\/p>\n<p>global_dict = {}<\/p>\n<p>def add_entry(key, value):<\/p>\n<p>    # \u4fee\u6539\u5168\u5c40\u5b57\u5178<\/p>\n<p>    global_dict[key] = value<\/p>\n<p>def print_dict():<\/p>\n<p>    # \u8bbf\u95ee\u5168\u5c40\u5b57\u5178<\/p>\n<p>    for key, value in global_dict.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>add_entry(&quot;name&quot;, &quot;Alice&quot;)<\/p>\n<p>print_dict()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>global_dict<\/code>\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5728<code>add_entry<\/code>\u548c<code>print_dict<\/code>\u51fd\u6570\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5168\u5c40\u5b57\u5178\u7684\u5b9a\u4e49\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5168\u5c40\u5b57\u5178\u7684\u5b9a\u4e49\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u5b57\u5178\u662f\u4e00\u79cd\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u5b9a\u4e49\u5b57\u5178\uff0c\u4f7f\u5176\u5728\u6574\u4e2a\u6a21\u5757\u5185\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u5c0f\u578b\u811a\u672c\u6216\u7b80\u5355\u7684\u6a21\u5757\uff0c\u4e0d\u9700\u8981\u590d\u6742\u7684\u547d\u540d\u7a7a\u95f4\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u7c7b\u5b9e\u73b0\u4e2d\uff0c\u6240\u6709\u5bf9\u5b57\u5178\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u90fd\u4e0d\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u56e0\u4e3a\u5b57\u5178\u672c\u8eab\u662f\u4e00\u4e2a\u53ef\u53d8\u5bf9\u8c61\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5168\u5c40\u5b57\u5178<\/p>\n<p>global_data = {}<\/p>\n<p>def modify_global_data(key, value):<\/p>\n<p>    # \u76f4\u63a5\u64cd\u4f5c\u5168\u5c40\u5b57\u5178<\/p>\n<p>    global_data[key] = value<\/p>\n<p>def read_global_data(key):<\/p>\n<p>    return global_data.get(key, &quot;Key not found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5168\u5c40\u5b57\u5178<\/strong><\/h2>\n<p>modify_global_data(&#39;age&#39;, 25)<\/p>\n<p>print(read_global_data(&#39;age&#39;))  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5b57\u5178<code>global_data<\/code>\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u53ef\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528global\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u4e00\u4e2a\u65b0\u7684\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u4f46\u5bf9\u4e8e\u5b57\u5178\u6765\u8bf4\uff0c\u53ea\u9700\u5728\u5b9a\u4e49\u65b0\u5b57\u5178\u65f6\u4f7f\u7528\u4e00\u6b21<code>global<\/code>\u5173\u952e\u5b57\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def init_global_dict():<\/p>\n<p>    global global_dict<\/p>\n<p>    global_dict = {&#39;initialized&#39;: True}<\/p>\n<p>def check_initialization():<\/p>\n<p>    return global_dict.get(&#39;initialized&#39;, False)<\/p>\n<p>init_global_dict()<\/p>\n<p>print(check_initialization())  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>init_global_dict<\/code>\u51fd\u6570\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5168\u5c40\u5b57\u5178\u3002\u8fd9\u4e2a\u5b57\u5178\u5728\u521d\u59cb\u5316\u540e\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8f83\u5927\u9879\u76ee\uff0c\u901a\u5e38\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u5b57\u5178\u6765\u7ec4\u7ec7\u6570\u636e\u3002\u901a\u8fc7\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u5b57\u5178\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u4f7f\u7528\uff0c\u7ef4\u6301\u826f\u597d\u7684\u4ee3\u7801\u7ed3\u6784\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\uff0c\u6bd4\u5982<code>config.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>settings = {<\/p>\n<p>    &#39;debug&#39;: True,<\/p>\n<p>    &#39;version&#39;: &#39;1.0.0&#39;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import config<\/p>\n<p>def toggle_debug_mode():<\/p>\n<p>    config.settings[&#39;debug&#39;] = not config.settings[&#39;debug&#39;]<\/p>\n<p>def print_version():<\/p>\n<p>    print(f&quot;Version: {config.settings[&#39;version&#39;]}&quot;)<\/p>\n<p>toggle_debug_mode()<\/p>\n<p>print_version()  # \u8f93\u51fa: Version: 1.0.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u6709\u52a9\u4e8e\u7ef4\u62a4\u4ee3\u7801\u7684\u6e05\u6670\u6027\u548c\u6a21\u5757\u5316\uff0c\u5c24\u5176\u5728\u5927\u578b\u9879\u76ee\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5168\u5c40\u5b57\u5178\u7684\u4f7f\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5168\u5c40\u5b57\u5178\u5728Python\u7f16\u7a0b\u4e2d\u6709\u591a\u79cd\u7528\u9014\uff0c\u5c24\u5176\u5728\u7ba1\u7406\u914d\u7f6e\u3001\u5171\u4eab\u6570\u636e\u4ee5\u53ca\u7f13\u5b58\u6570\u636e\u7b49\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h3>\u914d\u7f6e\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5168\u5c40\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\u3002\u901a\u8fc7\u5728\u4e00\u4e2a\u5730\u65b9\u96c6\u4e2d\u7ba1\u7406\u914d\u7f6e\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u548c\u4fee\u6539\u914d\u7f6e\u53c2\u6570\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 8080<\/p>\n<p>}<\/p>\n<p>def update_host(new_host):<\/p>\n<p>    config[&#39;host&#39;] = new_host<\/p>\n<p>def get_config():<\/p>\n<p>    return config<\/p>\n<p>update_host(&#39;127.0.0.1&#39;)<\/p>\n<p>print(get_config())  # \u8f93\u51fa: {&#39;host&#39;: &#39;127.0.0.1&#39;, &#39;port&#39;: 8080}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u5171\u4eab<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u51fd\u6570\u95f4\u5171\u4eab\u6570\u636e\u65f6\uff0c\u5168\u5c40\u5b57\u5178\u662f\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u591a\u4e2a\u51fd\u6570\u8bbf\u95ee\u548c\u4fee\u6539\u76f8\u540c\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7\u53c2\u6570\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">shared_data = {}<\/p>\n<p>def add_data(key, value):<\/p>\n<p>    shared_data[key] = value<\/p>\n<p>def get_data(key):<\/p>\n<p>    return shared_data.get(key)<\/p>\n<p>add_data(&#39;username&#39;, &#39;admin&#39;)<\/p>\n<p>print(get_data(&#39;username&#39;))  # \u8f93\u51fa: admin<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u51fd\u6570\u95f4\u7684\u6570\u636e\u5171\u4eab\uff0c\u800c\u4e0d\u5fc5\u62c5\u5fc3\u4f5c\u7528\u57df\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u7f13\u5b58\u673a\u5236<\/h3>\n<\/p>\n<p><p>\u5168\u5c40\u5b57\u5178\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u7f13\u5b58\u673a\u5236\uff0c\u5b58\u50a8\u8ba1\u7b97\u7ed3\u679c\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">cache = {}<\/p>\n<p>def expensive_computation(x):<\/p>\n<p>    if x in cache:<\/p>\n<p>        return cache[x]<\/p>\n<p>    result = x * x  # \u5047\u8bbe\u8fd9\u662f\u4e2a\u8017\u65f6\u7684\u8ba1\u7b97<\/p>\n<p>    cache[x] = result<\/p>\n<p>    return result<\/p>\n<p>print(expensive_computation(4))  # \u7b2c\u4e00\u6b21\u8ba1\u7b97<\/p>\n<p>print(expensive_computation(4))  # \u4f7f\u7528\u7f13\u5b58<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u5347\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5168\u5c40\u5b57\u5178\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u66f4\u6e05\u6670\u3001\u6613\u4e8e\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u5168\u5c40\u5b57\u5178\u5f88\u65b9\u4fbf\uff0c\u4f46\u8fc7\u5ea6\u4f7f\u7528\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002\u5f00\u53d1\u8005\u5e94\u8c28\u614e\u4f7f\u7528\u5168\u5c40\u5b57\u5178\uff0c\u5c24\u5176\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5c3d\u91cf\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6765\u4f20\u9012\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u547d\u540d\u89c4\u8303<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6e05\u6670\u7684\u547d\u540d\u89c4\u8303\u53ef\u4ee5\u5e2e\u52a9\u56e2\u961f\u6210\u5458\u7406\u89e3\u5b57\u5178\u7684\u7528\u9014\u3002\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u901a\u7528\u7684\u540d\u79f0\uff0c\u786e\u4fdd\u5b57\u5178\u540d\u79f0\u80fd\u51c6\u786e\u53cd\u6620\u5176\u7528\u9014\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u5757\u5316\u8bbe\u8ba1<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u901a\u8fc7\u6a21\u5757\u5316\u8bbe\u8ba1\u6765\u7ba1\u7406\u5168\u5c40\u5b57\u5178\u3002\u5c06\u76f8\u5173\u7684\u5168\u5c40\u5b57\u5178\u96c6\u4e2d\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u907f\u514d\u5728\u5404\u5904\u6563\u5e03\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u9501\u5b9a<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u65f6\uff0c\u9700\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u9501\u6765\u786e\u4fdd\u5bf9\u5b57\u5178\u7684\u5b89\u5168\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from threading import Lock<\/p>\n<p>global_dict = {}<\/p>\n<p>dict_lock = Lock()<\/p>\n<p>def thread_<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_add(key, value):<\/p>\n<p>    with dict_lock:<\/p>\n<p>        global_dict[key] = value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u9501\uff0c\u786e\u4fdd\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5bf9\u5168\u5c40\u5b57\u5178\u7684\u5b89\u5168\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5168\u5c40\u5b57\u5178\u7684\u66ff\u4ee3\u65b9\u6848<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5168\u5c40\u5b57\u5178\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5728\u590d\u6742\u9879\u76ee\u4e2d\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u8bbe\u8ba1\u6a21\u5f0f\u6765\u66ff\u4ee3\u5168\u5c40\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u8fd9\u5728\u9700\u8981\u5171\u4eab\u72b6\u6001\u6216\u914d\u7f6e\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\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>        self.data = {}<\/p>\n<p>singleton1 = Singleton()<\/p>\n<p>singleton1.data[&#39;key&#39;] = &#39;value&#39;<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p>print(singleton2.data[&#39;key&#39;])  # \u8f93\u51fa: value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u72b6\u6001\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u907f\u514d\u76f4\u63a5\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u914d\u7f6e\u7c7b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u914d\u7f6e\u7c7b\u6765\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ec4\u7ec7\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Config:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.settings = {<\/p>\n<p>            &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>            &#39;port&#39;: 8080<\/p>\n<p>        }<\/p>\n<p>    def update_host(self, new_host):<\/p>\n<p>        self.settings[&#39;host&#39;] = new_host<\/p>\n<p>    def get_config(self):<\/p>\n<p>        return self.settings<\/p>\n<p>config = Config()<\/p>\n<p>config.update_host(&#39;127.0.0.1&#39;)<\/p>\n<p>print(config.get_config())  # \u8f93\u51fa: {&#39;host&#39;: &#39;127.0.0.1&#39;, &#39;port&#39;: 8080}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u914d\u7f6e\u7ba1\u7406\u903b\u8f91\u5c01\u88c5\u5728\u7c7b\u4e2d\uff0c\u907f\u514d\u5168\u5c40\u5b57\u5178\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5168\u5c40\u5b57\u5178\u5728Python\u7f16\u7a0b\u4e2d\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u6570\u636e\u5171\u4eab\u65b9\u5f0f\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u4ee5\u53ca\u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u5b57\u5178\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u5168\u5c40\u6570\u636e\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u65f6\uff0c\u5e94\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\uff0c\u5e76\u8003\u8651\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u548c\u914d\u7f6e\u7c7b\u7b49\u66ff\u4ee3\u65b9\u6848\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u7ec4\u7ec7\u6027\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5168\u5c40\u5b57\u5178\uff0c\u53ef\u4ee5\u5728\u4fdd\u6301\u4ee3\u7801\u7b80\u6d01\u7684\u540c\u65f6\uff0c\u5b9e\u73b0\u9ad8\u6548\u7684\u6570\u636e\u5171\u4eab\u548c\u7ba1\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u6765\u521b\u5efa\u5168\u5c40\u5b57\u5178\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u60a8\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7684\u4efb\u4f55\u5730\u65b9\u8bbf\u95ee\u548c\u4fee\u6539\u8fd9\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">global_dict = {}\n\ndef add_to_dict(key, value):\n    global global_dict\n    global_dict[key] = value\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\uff0c\u60a8\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u4fee\u6539\u5168\u5c40\u5b57\u5178\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5728\u5168\u5c40\u5b57\u5178\u4e2d\u5982\u4f55\u5b58\u50a8\u591a\u4e2a\u503c\uff1f<\/strong><br \/>\u5168\u5c40\u5b57\u5178\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7ed3\u6784\u6765\u5b58\u50a8\u591a\u4e2a\u503c\u3002\u53ef\u4ee5\u5c06\u503c\u5b9a\u4e49\u4e3a\u5217\u8868\u3001\u5143\u7ec4\u6216\u53e6\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">global_dict = {\n    &#39;fruits&#39;: [&#39;apple&#39;, &#39;banana&#39;],\n    &#39;vegetables&#39;: {&#39;carrot&#39;: 3, &#39;broccoli&#39;: 5}\n}\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b58\u53d6\u548c\u7ba1\u7406\u591a\u4e2a\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u51fd\u6570\u4e2d\u8bbf\u95ee\u5168\u5c40\u5b57\u5178\u7684\u503c\uff1f<\/strong><br \/>\u8981\u5728\u4e0d\u540c\u7684\u51fd\u6570\u4e2d\u8bbf\u95ee\u5168\u5c40\u5b57\u5178\u7684\u503c\uff0c\u60a8\u53ea\u9700\u5728\u51fd\u6570\u4e2d\u58f0\u660e\u5b57\u5178\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u7136\u540e\u8bfb\u53d6\u5176\u5185\u5bb9\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def get_fruit():\n    global global_dict\n    return global_dict[&#39;fruits&#39;]\n\nprint(get_fruit())  # \u8f93\u51fa\uff1a[&#39;apple&#39;, &#39;banana&#39;]\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u4efb\u4f55\u51fd\u6570\u4e2d\u8f7b\u677e\u8bbf\u95ee\u5168\u5c40\u5b57\u5178\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u5168\u5c40\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5916\u90e8\u58f0\u660e\u5b57\u5178\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u5b57\u5178\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u8fd9 [&hellip;]","protected":false},"author":3,"featured_media":966893,"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\/966883"}],"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=966883"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966883\/revisions"}],"predecessor-version":[{"id":966894,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/966883\/revisions\/966894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/966893"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=966883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=966883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=966883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}