{"id":1103983,"date":"2025-01-08T16:16:06","date_gmt":"2025-01-08T08:16:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1103983.html"},"modified":"2025-01-08T16:16:09","modified_gmt":"2025-01-08T08:16:09","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e5%85%b8%e5%a3%b0%e6%98%8e%e4%b8%ba%e5%85%a8%e5%b1%80%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1103983.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5b57\u5178\u58f0\u660e\u4e3a\u5168\u5c40\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065543\/c542b67c-ad1e-4920-a022-bdaa6cac775b.webp\" alt=\"python\u5982\u4f55\u5c06\u5b57\u5178\u58f0\u660e\u4e3a\u5168\u5c40\u7684\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178\u3001\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9a\u4e49\u548c\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u7b49\u65b9\u6cd5\u3002<\/strong>\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528global\u5173\u952e\u5b57\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u8f83\u4e3a\u5e38\u89c1\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u5e76\u7ed9\u51fa\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u58f0\u660e\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528global\u5173\u952e\u5b57\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u5bf9\u5168\u5c40\u53d8\u91cf\u8fdb\u884c\u4fee\u6539\u548c\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>def update_dict():<\/p>\n<p>    global my_dict<\/p>\n<p>    my_dict[&quot;key1&quot;] = &quot;new_value&quot;<\/p>\n<p>update_dict()<\/p>\n<p>print(my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7global\u5173\u952e\u5b57\u58f0\u660emy_dict\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u5728\u51fd\u6570\u5185\u90e8\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u8be5\u5b57\u5178\u800c\u65e0\u9700\u4f7f\u7528global\u5173\u952e\u5b57\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u51cf\u5c11\u4e86\u4ee3\u7801\u7684\u590d\u6742\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>def access_dict():<\/p>\n<p>    print(my_dict[&quot;key1&quot;])<\/p>\n<p>access_dict()  # \u8f93\u51fa: value1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cmy_dict\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\uff0c\u56e0\u6b64\u5728\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8be5\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9a\u4e49\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u5c5e\u6027\u5b9a\u4e49\u5168\u5c40\u5b57\u5178\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u5178\u4f5c\u4e3a\u7c7b\u7684\u5c5e\u6027\uff0c\u4f7f\u5176\u5728\u7c7b\u7684\u4e0d\u540c\u65b9\u6cd5\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>    @classmethod<\/p>\n<p>    def update_dict(cls):<\/p>\n<p>        cls.my_dict[&quot;key1&quot;] = &quot;new_value&quot;<\/p>\n<p>MyClass.update_dict()<\/p>\n<p>print(MyClass.my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cmy_dict\u4f5c\u4e3a\u7c7b\u7684\u5c5e\u6027\u5b9a\u4e49\uff0c\u5e76\u901a\u8fc7\u7c7b\u65b9\u6cd5\u66f4\u65b0\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u7ea7\u522b\u5b9a\u4e49\u5b57\u5178\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6a21\u5757\u7684\u4e0d\u540c\u51fd\u6570\u548c\u7c7b\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u8be5\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8f83\u5927\u7684\u9879\u76ee\uff0c\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u53ef\u4ee5\u88ab\u5176\u4ed6\u6a21\u5757\u5bfc\u5165\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module.py<\/p>\n<p>my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>def update_dict():<\/p>\n<p>    global my_dict<\/p>\n<p>    my_dict[&quot;key1&quot;] = &quot;new_value&quot;<\/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<\/p>\n<p>module.update_dict()<\/p>\n<p>print(module.my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0cmy_dict\u5728module.py\u4e2d\u5b9a\u4e49\uff0c\u5e76\u5728main.py\u4e2d\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u8bbf\u95ee\u548c\u4fee\u6539\u8be5\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u5b9a\u4e49\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u5b57\u5178\uff0c\u4f7f\u5176\u5728\u7a0b\u5e8f\u4e2d\u59cb\u7ec8\u4fdd\u6301\u552f\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/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>singleton = Singleton()<\/p>\n<p>singleton.my_dict[&quot;key1&quot;] = &quot;new_value&quot;<\/p>\n<p>singleton2 = Singleton()<\/p>\n<p>print(singleton2.my_dict)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5355\u4f8b\u6a21\u5f0f\u786e\u4fdd\u7c7bSingleton\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4ece\u800c\u5b9e\u73b0\u5168\u5c40\u5b57\u5178\u7684\u5b9a\u4e49\u548c\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u5b58\u50a8\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06\u5b57\u5178\u5b58\u50a8\u5728\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u5168\u5c40\u8bbf\u95ee\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u73af\u5883\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u5b58\u50a8\u7b80\u5355\u7684\u952e\u503c\u5bf9\uff0c\u56e0\u6b64\u5728\u5b58\u50a8\u590d\u6742\u6570\u636e\u7ed3\u6784\u65f6\u9700\u8981\u8fdb\u884c\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import json<\/p>\n<p>my_dict = {&quot;key1&quot;: &quot;value1&quot;, &quot;key2&quot;: &quot;value2&quot;}<\/p>\n<p>os.environ[&quot;MY_DICT&quot;] = json.dumps(my_dict)<\/p>\n<p>def update_dict():<\/p>\n<p>    global my_dict<\/p>\n<p>    my_dict = json.loads(os.environ[&quot;MY_DICT&quot;])<\/p>\n<p>    my_dict[&quot;key1&quot;] = &quot;new_value&quot;<\/p>\n<p>    os.environ[&quot;MY_DICT&quot;] = json.dumps(my_dict)<\/p>\n<p>update_dict()<\/p>\n<p>print(json.loads(os.environ[&quot;MY_DICT&quot;]))  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7json\u6a21\u5757\u5c06\u5b57\u5178\u5e8f\u5217\u5316\u5b58\u50a8\u5728\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u66f4\u65b0\u5b57\u5178\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u5b58\u50a8\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u8f83\u5927\u7684\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u5b58\u50a8\u5168\u5c40\u5b57\u5178\uff0c\u4ece\u800c\u5b9e\u73b0\u5168\u5c40\u8bbf\u95ee\u3002\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u91c7\u7528JSON\u3001YAML\u6216INI\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.json<\/p>\n<p>{<\/p>\n<p>    &quot;key1&quot;: &quot;value1&quot;,<\/p>\n<p>    &quot;key2&quot;: &quot;value2&quot;<\/p>\n<p>}<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import json<\/p>\n<p>def load_config(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        return json.load(file)<\/p>\n<p>def update_dict(config):<\/p>\n<p>    config[&quot;key1&quot;] = &quot;new_value&quot;<\/p>\n<p>    with open(&#39;config.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>        json.dump(config, file)<\/p>\n<p>config = load_config(&#39;config.json&#39;)<\/p>\n<p>update_dict(config)<\/p>\n<p>config = load_config(&#39;config.json&#39;)<\/p>\n<p>print(config)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7JSON\u914d\u7f6e\u6587\u4ef6\u5b58\u50a8\u5168\u5c40\u5b57\u5178\uff0c\u5e76\u5728\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u548c\u66f4\u65b0\u8be5\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u6570\u636e\u5e93\u5b58\u50a8\u5168\u5c40\u5b57\u5178<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5c06\u5168\u5c40\u5b57\u5178\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u4ece\u800c\u5b9e\u73b0\u5168\u5c40\u8bbf\u95ee\u3002\u6570\u636e\u5e93\u53ef\u4ee5\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MySQL\u3001PostgreSQL\uff09\u6216\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MongoDB\u3001Redis\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def create_table():<\/p>\n<p>    conn = sqlite3.connect(&#39;config.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS config<\/p>\n<p>                      (key TEXT PRIMARY KEY, value TEXT)&#39;&#39;&#39;)<\/p>\n<p>    cursor.execute(&quot;INSERT OR REPLACE INTO config (key, value) VALUES (&#39;key1&#39;, &#39;value1&#39;), (&#39;key2&#39;, &#39;value2&#39;)&quot;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def update_dict():<\/p>\n<p>    conn = sqlite3.connect(&#39;config.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;UPDATE config SET value = &#39;new_value&#39; WHERE key = &#39;key1&#39;&quot;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>def load_config():<\/p>\n<p>    conn = sqlite3.connect(&#39;config.db&#39;)<\/p>\n<p>    cursor = conn.cursor()<\/p>\n<p>    cursor.execute(&quot;SELECT * FROM config&quot;)<\/p>\n<p>    config = {row[0]: row[1] for row in cursor.fetchall()}<\/p>\n<p>    conn.close()<\/p>\n<p>    return config<\/p>\n<p>create_table()<\/p>\n<p>update_dict()<\/p>\n<p>config = load_config()<\/p>\n<p>print(config)  # \u8f93\u51fa: {&#39;key1&#39;: &#39;new_value&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7SQLite\u6570\u636e\u5e93\u5b58\u50a8\u5168\u5c40\u5b57\u5178\uff0c\u5e76\u5728\u7a0b\u5e8f\u4e2d\u52a0\u8f7d\u548c\u66f4\u65b0\u8be5\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5305\u62ec\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178\u3001\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9a\u4e49\u3001\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3001\u5355\u4f8b\u6a21\u5f0f\u3001\u73af\u5883\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e93\u7b49\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178\u548c\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9a\u4e49\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u9879\u76ee\uff0c\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3001\u914d\u7f6e\u6587\u4ef6\u548c\u6570\u636e\u5e93\u5b58\u50a8\u5168\u5c40\u5b57\u5178\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u548c\u73af\u5883\u53d8\u91cf\u5b58\u50a8\u5168\u5c40\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u58f0\u660e\u548c\u8bbf\u95ee\u5168\u5c40\u5b57\u5178\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u5904\u7406\u5168\u5c40\u5b57\u5178\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u5b57\u5178\uff1f<\/strong><br \/>\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u5b57\u5178\uff0c\u60a8\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u5b57\u5178\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u544a\u8bc9Python\u60a8\u60f3\u8981\u5f15\u7528\u7684\u662f\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u5b57\u5178\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {}\n\ndef update_dict(key, value):\n    global my_dict\n    my_dict[key] = value\n\nupdate_dict(&#39;a&#39;, 1)\nprint(my_dict)  # \u8f93\u51fa: {&#39;a&#39;: 1}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u5bf9\u5168\u5c40\u5b57\u5178\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u9700\u8981\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u800c\u4e0d\u662f\u5c40\u90e8\u5b57\u5178\uff1f<\/strong><br \/>\u4f7f\u7528\u5168\u5c40\u5b57\u5178\u901a\u5e38\u5728\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u82e5\u60a8\u6709\u4e00\u4e2a\u914d\u7f6e\u5b57\u5178\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u6216\u8005\u5728\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7ef4\u62a4\u72b6\u6001\u65f6\uff0c\u5168\u5c40\u5b57\u5178\u53ef\u4ee5\u7b80\u5316\u6570\u636e\u7ba1\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u907f\u514d\u5168\u5c40\u5b57\u5178\u5f15\u53d1\u7684\u6f5c\u5728\u95ee\u9898\uff1f<\/strong><br \/>\u5168\u5c40\u5b57\u5178\u7684\u4f7f\u7528\u867d\u7136\u65b9\u4fbf\uff0c\u4f46\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\u3002\u4e3a\u4e86\u907f\u514d\u6f5c\u5728\u95ee\u9898\uff0c\u5efa\u8bae\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u4f20\u9012\u5b57\u5178\uff0c\u6216\u8003\u8651\u4f7f\u7528\u7c7b\u6765\u5c01\u88c5\u5b57\u5178\u548c\u76f8\u5173\u64cd\u4f5c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u540c\u65f6\u51cf\u5c11\u610f\u5916\u4fee\u6539\u5168\u5c40\u72b6\u6001\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u5b57\u5178\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b57\u5178\u3001\u901a\u8fc7\u7c7b\u5c5e\u6027\u5b9a\u4e49\u548c [&hellip;]","protected":false},"author":3,"featured_media":1103994,"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\/1103983"}],"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=1103983"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103983\/revisions"}],"predecessor-version":[{"id":1103999,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1103983\/revisions\/1103999"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1103994"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1103983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1103983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1103983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}