{"id":1110999,"date":"2025-01-08T17:25:03","date_gmt":"2025-01-08T09:25:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110999.html"},"modified":"2025-01-08T17:25:06","modified_gmt":"2025-01-08T09:25:06","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%87%bd%e6%95%b0%e5%86%85%e4%bd%bf%e7%94%a8%e5%85%a8%e5%b1%80%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110999.html","title":{"rendered":"python\u5982\u4f55\u5728\u51fd\u6570\u5185\u4f7f\u7528\u5168\u5c40\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073618\/dc04f96d-31ab-4452-b971-367484b93e26.webp\" alt=\"python\u5982\u4f55\u5728\u51fd\u6570\u5185\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6709\u51e0\u79cd\u65b9\u5f0f\uff1a<strong>\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e<\/strong>\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5728\u51fd\u6570\u5185\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u6df1\u5165\u63a2\u8ba8\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528global\u5173\u952e\u5b57<\/strong>\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5728\u51fd\u6570\u5185\u4f7f\u7528global\u5173\u952e\u5b57\u53ef\u4ee5\u544a\u8bc9Python\u89e3\u91ca\u5668\uff0c\u8fd9\u4e2a\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u5c40\u90e8\u53d8\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>def my_function():<\/p>\n<p>    global x<\/p>\n<p>    x = 20<\/p>\n<p>my_function()<\/p>\n<p>print(x)  # \u8f93\u51fa 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728\u51fd\u6570<code>my_function<\/code>\u5185\u58f0\u660e\u4e86<code>global x<\/code>\uff0c\u8fd9\u610f\u5473\u7740<code>x<\/code>\u6307\u7684\u662f\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u6837\uff0c<code>my_function<\/code>\u5c31\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528global\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p><strong>1\u3001\u58f0\u660e\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u5982\u679c\u60f3\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981\u7528\u5230global\u5173\u952e\u5b57\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u51fd\u6570\u5185\u7684\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf\u6307\u5411\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>def increment():<\/p>\n<p>    global count<\/p>\n<p>    count += 1<\/p>\n<p>increment()<\/p>\n<p>print(count)  # \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002\u5728\u51fd\u6570<code>increment<\/code>\u5185\uff0c\u6211\u4eec\u4f7f\u7528<code>global count<\/code>\u6765\u58f0\u660e<code>count<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u4f7f\u7528global\u5173\u952e\u5b57\u662f\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u662f\u4e0d\u63a8\u8350\u7684\u3002\u5168\u5c40\u53d8\u91cf\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u4f7f\u5f97\u8c03\u8bd5\u548c\u7ef4\u62a4\u53d8\u5f97\u66f4\u52a0\u56f0\u96be\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u53d8\u91cf\u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u8fdb\u884c\u4f20\u9012\uff0c\u6216\u8005\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def increment(counter):<\/p>\n<p>    return counter + 1<\/p>\n<p>count = 0<\/p>\n<p>count = increment(count)<\/p>\n<p>print(count)  # \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5c06\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u907f\u514d\u4e86\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p><strong>1\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u4f20\u9012\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5c06\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u662f\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u4e00\u79cd\u6709\u6548\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u51cf\u5c11\u53d8\u91cf\u4e4b\u95f4\u7684\u8026\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(5, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570<code>add<\/code>\u7684\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u907f\u514d\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u8fd8\u4f7f\u5f97\u51fd\u6570\u66f4\u52a0\u901a\u7528\u548c\u6613\u4e8e\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7ba1\u7406\u72b6\u6001<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u72b6\u6001\u662f\u53e6\u4e00\u79cd\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u65b9\u6cd5\u3002\u7c7b\u53ef\u4ee5\u5c01\u88c5\u6570\u636e\u548c\u65b9\u6cd5\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7ed3\u6784\u5316\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>    def increment(self):<\/p>\n<p>        self.count += 1<\/p>\n<p>counter = Counter()<\/p>\n<p>counter.increment()<\/p>\n<p>print(counter.count)  # \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Counter<\/code>\u7c7b\uff0c\u7528\u4e8e\u7ba1\u7406\u8ba1\u6570\u5668\u7684\u72b6\u6001\u3002\u901a\u8fc7\u5c06\u72b6\u6001\u5c01\u88c5\u5728\u7c7b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e<\/h3>\n<\/p>\n<p><p><strong>1\u3001\u5728\u6a21\u5757\u4e2d\u5171\u4eab\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e\u662f\u5408\u7406\u7684\u3002\u4f8b\u5982\uff0c\u5f53\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u72b6\u6001\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">shared_data = {}<\/p>\n<p>def add_data(key, value):<\/p>\n<p>    global shared_data<\/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;name&#39;, &#39;Alice&#39;)<\/p>\n<p>print(get_data(&#39;name&#39;))  # \u8f93\u51fa Alice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<code>shared_data<\/code>\u6765\u5171\u4eab\u6570\u636e\u3002\u901a\u8fc7<code>add_data<\/code>\u548c<code>get_data<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e\u3002\u7136\u800c\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5bf9\u5168\u5c40\u53d8\u91cf\u7684\u8bbf\u95ee\u9700\u8981\u4f7f\u7528\u9501\uff08Lock\uff09\u6765\u907f\u514d\u7ade\u4e89\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>count = 0<\/p>\n<p>lock = threading.Lock()<\/p>\n<p>def increment():<\/p>\n<p>    global count<\/p>\n<p>    with lock:<\/p>\n<p>        count += 1<\/p>\n<p>threads = [threading.Thread(target=increment) for _ in range(100)]<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.start()<\/p>\n<p>for thread in threads:<\/p>\n<p>    thread.join()<\/p>\n<p>print(count)  # \u8f93\u51fa 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u9501\u6765\u4fdd\u62a4\u5bf9\u5168\u5c40\u53d8\u91cf<code>count<\/code>\u7684\u8bbf\u95ee\uff0c\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5168\u5c40\u53d8\u91cf\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p><strong>1\u3001\u4f18\u70b9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5316\u6570\u636e\u5171\u4eab<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u7b80\u5316\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u7684\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u65b9\u4fbf\u5feb\u901f\u5b9e\u73b0<\/strong>\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0\u6240\u9700\u7684\u529f\u80fd\uff0c\u800c\u65e0\u9700\u590d\u6742\u7684\u8bbe\u8ba1\u3002<\/li>\n<\/ul>\n<p><p><strong>2\u3001\u7f3a\u70b9<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u589e\u52a0\u4ee3\u7801\u590d\u6742\u6027<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u53d8\u5f97\u96be\u4ee5\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u6613\u5f15\u5165\u9519\u8bef<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u5bb9\u6613\u5bfc\u81f4\u96be\u4ee5\u8ffd\u8e2a\u7684\u9519\u8bef\uff0c\u7279\u522b\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002<\/li>\n<li><strong>\u4e0d\u5229\u4e8e\u6d4b\u8bd5<\/strong>\uff1a\u5168\u5c40\u53d8\u91cf\u4f7f\u5f97\u5355\u5143\u6d4b\u8bd5\u53d8\u5f97\u56f0\u96be\uff0c\u56e0\u4e3a\u6d4b\u8bd5\u7528\u4f8b\u4e4b\u95f4\u53ef\u80fd\u4f1a\u76f8\u4e92\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p><strong>1\u3001\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5168\u5c40\u53d8\u91cf\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u662f\u5fc5\u8981\u7684\uff0c\u4f46\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5e94\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u72b6\u6001\uff0c\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>2\u3001\u4f7f\u7528\u547d\u540d\u7a7a\u95f4<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5fc5\u987b\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u53d8\u91cf\u4e4b\u95f4\u7684\u51b2\u7a81\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class GlobalVars:<\/p>\n<p>    count = 0<\/p>\n<p>def increment():<\/p>\n<p>    GlobalVars.count += 1<\/p>\n<p>increment()<\/p>\n<p>print(GlobalVars.count)  # \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>GlobalVars<\/code>\u7c7b\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u51cf\u5c11\u53d8\u91cf\u4e4b\u95f4\u7684\u51b2\u7a81\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p><strong>3\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u5168\u5c40\u53d8\u91cf\u3002\u901a\u8fc7\u5c06\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u6613\u4e8e\u7ba1\u7406\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>config = {<\/p>\n<p>    &#39;count&#39;: 0<\/p>\n<p>}<\/p>\n<p>def increment():<\/p>\n<p>    config[&#39;count&#39;] += 1<\/p>\n<p>increment()<\/p>\n<p>print(config[&#39;count&#39;])  # \u8f93\u51fa 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5b57\u5178<code>config<\/code>\u6765\u5b58\u50a8\u5168\u5c40\u53d8\u91cf\u3002\u901a\u8fc7\u5c06\u5168\u5c40\u53d8\u91cf\u5b58\u50a8\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u6613\u4e8e\u7ba1\u7406\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5728Python\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4f46\u5e94\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u72b6\u6001\uff0c\u53ef\u4ee5\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5168\u5c40\u53d8\u91cf\u662f\u5fc5\u8981\u7684\uff0c\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u6765\u51cf\u5c11\u5176\u5e26\u6765\u7684\u590d\u6742\u6027\u548c\u6f5c\u5728\u9519\u8bef\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\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5728\u51fd\u6570\u5185\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u4fee\u6539\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u60a8\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u8be5\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u6837\u60a8\u5c31\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 10\n\ndef modify_global():\n    global x\n    x += 5\n\nmodify_global()\nprint(x)  # \u8f93\u51fa 15\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u65f6\uff0c\u6709\u54ea\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><br \/>\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff0c\u4f46\u4e5f\u5e26\u6765\u4e86\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u7684\u95ee\u9898\u3002\u8fc7\u5ea6\u4f9d\u8d56\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u7684\u4fee\u6539\u548c\u96be\u4ee5\u8ffd\u8e2a\u7684\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u5728\u5fc5\u8981\u65f6\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u5c3d\u91cf\u4fdd\u6301\u51fd\u6570\u7684\u72ec\u7acb\u6027\u548c\u81ea\u6211\u5c01\u95ed\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u51fd\u6570\u5185\u5f15\u7528\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u4fee\u6539\u5b83\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u53ea\u662f\u60f3\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u800c\u4e0d\u8fdb\u884c\u4fee\u6539\uff0c\u53ef\u4ee5\u76f4\u63a5\u5f15\u7528\u8be5\u53d8\u91cf\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">y = 20\n\ndef use_global():\n    print(y)\n\nuse_global()  # \u8f93\u51fa 20\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u6301\u5168\u5c40\u53d8\u91cf\u7684\u539f\u59cb\u503c\u4e0d\u53d8\uff0c\u540c\u65f6\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6709\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1111013,"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\/1110999"}],"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=1110999"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110999\/revisions"}],"predecessor-version":[{"id":1111015,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110999\/revisions\/1111015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111013"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}