{"id":1030006,"date":"2024-12-31T11:13:20","date_gmt":"2024-12-31T03:13:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1030006.html"},"modified":"2024-12-31T11:13:24","modified_gmt":"2024-12-31T03:13:24","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e5%a2%9e%e5%8a%a0%e5%b1%80%e9%83%a8%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1030006.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u589e\u52a0\u5c40\u90e8\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0f474e0f-daa9-4224-88bc-dda03c18d032.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u5229\u7528\u95ed\u5305\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\uff0c\u8fd9\u6837\u58f0\u660e\u7684\u53d8\u91cf\u4ec5\u5728\u51fd\u6570\u5185\u90e8\u6709\u6548\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40\u53d8\u91cf\u3002\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8d4b\u503c\u64cd\u4f5c\u6765\u8fdb\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    local_var = 10<\/p>\n<p>    print(local_var)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c<code>local_var<\/code>\u5c31\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728<code>my_function<\/code>\u51fd\u6570\u5185\u90e8\u6709\u6548\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5728Python\u4e2d\u5982\u4f55\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\uff0c\u4ee5\u53ca\u5c40\u90e8\u53d8\u91cf\u5728\u4e0d\u540c\u7684\u573a\u666f\u4e0b\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5c40\u90e8\u53d8\u91cf\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\uff0c\u5e76\u4e14\u5b83\u4eec\u5728\u51fd\u6570\u7ed3\u675f\u65f6\u4f1a\u88ab\u9500\u6bc1\u3002\u5c40\u90e8\u53d8\u91cf\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_numbers(a, b):<\/p>\n<p>    result = a + b  # result \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(&quot;Sum:&quot;, result)<\/p>\n<p>add_numbers(5, 7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>result<\/code>\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728<code>add_numbers<\/code>\u51fd\u6570\u5185\u90e8\u6709\u6548\u3002\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\uff0c<code>result<\/code>\u53d8\u91cf\u5c31\u4e0d\u518d\u5b58\u5728\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u51fd\u6570\u53c2\u6570\u4e5f\u662f\u4e00\u79cd\u5c40\u90e8\u53d8\u91cf\u3002\u5b83\u4eec\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u58f0\u660e\uff0c\u5e76\u4e14\u5728\u51fd\u6570\u8c03\u7528\u65f6\u8d4b\u503c\u3002\u53c2\u6570\u5728\u51fd\u6570\u5185\u90e8\u8d77\u5230\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    message = f&quot;Hello, {name}!&quot;  # message \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(message)<\/p>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>name<\/code>\u53c2\u6570\u548c<code>message<\/code>\u53d8\u91cf\u90fd\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728<code>greet<\/code>\u51fd\u6570\u5185\u90e8\u6709\u6548\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5229\u7528\u95ed\u5305<\/h2>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u60c5\u51b5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u5916\u90e8\u51fd\u6570\u5185\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\uff0c\u5e76\u901a\u8fc7\u5185\u90e8\u51fd\u6570\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\uff0c\u4f7f\u5176\u5728\u5916\u90e8\u51fd\u6570\u7ed3\u675f\u540e\u4f9d\u7136\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def inner_function(y):<\/p>\n<p>        return x + y  # x \u662f\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0cinner_function \u95ed\u5305\u53ef\u4ee5\u8bbf\u95ee\u5b83<\/p>\n<p>    return inner_function<\/p>\n<p>closure = outer_function(10)<\/p>\n<p>print(closure(5))  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code>\u662f<code>outer_function<\/code>\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u901a\u8fc7\u95ed\u5305\u673a\u5236\uff0c<code>inner_function<\/code>\u53ef\u4ee5\u8bbf\u95ee\u5e76\u4f7f\u7528<code>x<\/code>\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/h2>\n<\/p>\n<p><p>\u7406\u89e3\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u5bf9\u7f16\u5199\u6b63\u786e\u7684Python\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u58f0\u660e\u5b83\u4eec\u7684\u51fd\u6570\u5185\u90e8\u3002\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u5168\u5c40\u53d8\u91cf\uff0c\u4f46\u8fd9\u4f1a\u7834\u574f\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u89c4\u5219\uff0c\u4e0d\u5efa\u8bae\u9891\u7e41\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def my_function():<\/p>\n<p>    x = 5  # \u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(&quot;Inside function:&quot;, x)<\/p>\n<p>my_function()<\/p>\n<p>print(&quot;Outside function:&quot;, x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u5185\u90e8\u7684<code>x<\/code>\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u5f71\u54cd\u5168\u5c40\u53d8\u91cf<code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u9012\u5f52<\/h2>\n<\/p>\n<p><p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u5728\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u65f6\u90fd\u4f1a\u6709\u72ec\u7acb\u7684\u526f\u672c\u3002\u7406\u89e3\u8fd9\u4e00\u70b9\u5bf9\u7f16\u5199\u9012\u5f52\u51fd\u6570\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        result = n * factorial(n - 1)  # result \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>        return result<\/p>\n<p>print(factorial(5))  # \u8f93\u51fa 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u6b21\u9012\u5f52\u8c03\u7528<code>factorial<\/code>\u51fd\u6570\u65f6\uff0c<code>result<\/code>\u53d8\u91cf\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u9012\u5f52\u8c03\u7528\u4e2d\u7684<code>result<\/code>\u53d8\u91cf\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5f02\u5e38\u5904\u7406<\/h2>\n<\/p>\n<p><p>\u5728\u5f02\u5e38\u5904\u7406\u4ee3\u7801\u5757\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e5f\u4ec5\u9650\u4e8e\u8be5\u4ee3\u7801\u5757\u3002\u7406\u89e3\u8fd9\u4e00\u70b9\u5bf9\u7f16\u5199\u5065\u58ee\u7684\u9519\u8bef\u5904\u7406\u4ee3\u7801\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b  # result \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        result = None  # result \u4f5c\u7528\u57df\u4ecd\u5728 except \u4ee3\u7801\u5757\u5185<\/p>\n<p>    return result<\/p>\n<p>print(divide(10, 2))  # \u8f93\u51fa 5.0<\/p>\n<p>print(divide(10, 0))  # \u8f93\u51fa None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>result<\/code>\u53d8\u91cf\u5728<code>try<\/code>\u548c<code>except<\/code>\u4ee3\u7801\u5757\u4e2d\u90fd\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u5f71\u54cd\u51fd\u6570\u5916\u90e8\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u591a\u7ebf\u7a0b<\/h2>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u5c40\u90e8\u53d8\u91cf\u4e0d\u4f1a\u5728\u4e0d\u540c\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\uff0c\u8fd9\u4fdd\u8bc1\u4e86\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def worker(num):<\/p>\n<p>    local_var = num * 2  # local_var \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(f&quot;Thread {num}: {local_var}&quot;)<\/p>\n<p>threads = []<\/p>\n<p>for i in range(5):<\/p>\n<p>    t = threading.Thread(target=worker, args=(i,))<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684<code>local_var<\/code>\u5c40\u90e8\u53d8\u91cf\uff0c\u4e0d\u4f1a\u4e0e\u5176\u4ed6\u7ebf\u7a0b\u7684\u53d8\u91cf\u51b2\u7a81\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u6027\u80fd<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002\u5c40\u90e8\u53d8\u91cf\u5b58\u50a8\u5728\u6808\u4e2d\uff0c\u8bbf\u95ee\u901f\u5ea6\u6bd4\u5168\u5c40\u53d8\u91cf\u548c\u5b9e\u4f8b\u53d8\u91cf\u66f4\u5feb\u3002\u5408\u7406\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compute_sum(n):<\/p>\n<p>    total = 0  # total \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    for i in range(n):<\/p>\n<p>        total += i<\/p>\n<p>    return total<\/p>\n<p>print(compute_sum(1000000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>total<\/code>\u53d8\u91cf\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u5b58\u50a8\u5728\u6808\u4e2d\uff0c\u8bbf\u95ee\u901f\u5ea6\u5feb\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5185\u5b58\u7ba1\u7406<\/h2>\n<\/p>\n<p><p>\u5c40\u90e8\u53d8\u91cf\u5728\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\u4f1a\u88ab\u9500\u6bc1\uff0cPython\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u81ea\u52a8\u56de\u6536\u8fd9\u4e9b\u53d8\u91cf\u5360\u7528\u7684\u5185\u5b58\u3002\u5408\u7406\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u5185\u5b58\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_large_list():<\/p>\n<p>    large_list = [i for i in range(1000000)]  # large_list \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    return sum(large_list)<\/p>\n<p>print(create_large_list())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>large_list<\/code>\u53d8\u91cf\u5728\u51fd\u6570\u7ed3\u675f\u540e\u4f1a\u88ab\u9500\u6bc1\uff0c\u5185\u5b58\u4f1a\u88ab\u56de\u6536\uff0c\u907f\u514d\u4e86\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u8c03\u8bd5<\/h2>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u7406\u89e3\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u548c\u751f\u547d\u5468\u671f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002\u8c03\u8bd5\u5de5\u5177\u901a\u5e38\u5141\u8bb8\u6211\u4eec\u67e5\u770b\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_area(radius):<\/p>\n<p>    pi = 3.14159  # pi \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    area = pi * radius  2<\/p>\n<p>    return area<\/p>\n<p>print(calculate_area(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u8c03\u8bd5\u5de5\u5177\u67e5\u770b<code>pi<\/code>\u548c<code>area<\/code>\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9a8c\u8bc1\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0c\u5c40\u90e8\u53d8\u91cf\u5728Python\u7f16\u7a0b\u4e2d\u8d77\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u5229\u7528\u95ed\u5305\u7b49\u65b9\u6cd5\u90fd\u53ef\u4ee5\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\u3002\u7406\u89e3\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3001\u751f\u547d\u5468\u671f\u548c\u4f7f\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u5065\u58ee\u7684Python\u4ee3\u7801\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3001\u4f18\u5316\u5185\u5b58\u7ba1\u7406\u3001\u7b80\u5316\u8c03\u8bd5\u8fc7\u7a0b\uff0c\u786e\u4fdd\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u5c40\u90e8\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u51fd\u6570\u6216\u4ee3\u7801\u5757\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8d4b\u503c\u64cd\u4f5c\u6765\u521b\u5efa\u5c40\u90e8\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u5b9a\u4e49\u53d8\u91cf\u65f6\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u4ec5\u9650\u4e8e\u8be5\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function():\n    local_var = 10  # \u8fd9\u662f\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\n    print(local_var)\n\nmy_function()  # \u8f93\u51fa: 10\nprint(local_var)  # \u8fd9\u5c06\u5f15\u53d1\u9519\u8bef\uff0c\u56e0\u4e3alocal_var\u4e0d\u5728\u5168\u5c40\u8303\u56f4\u5185\n<\/code><\/pre>\n<p><strong>\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5c40\u90e8\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u9650\u5236\u5728\u5b83\u88ab\u5b9a\u4e49\u7684\u51fd\u6570\u6216\u4ee3\u7801\u5757\u5185\u3002\u8fd9\u610f\u5473\u7740\u5728\u51fd\u6570\u5916\u90e8\u65e0\u6cd5\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u907f\u514d\u547d\u540d\u51b2\u7a81\uff0c\u5e76\u4e14\u5e2e\u52a9\u7ba1\u7406\u5185\u5b58\uff0c\u56e0\u4e3a\u5c40\u90e8\u53d8\u91cf\u53ea\u5728\u51fd\u6570\u6267\u884c\u65f6\u5b58\u5728\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u591a\u4e2a\u51fd\u6570\u95f4\u5171\u4eab\u53d8\u91cf\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u591a\u4e2a\u51fd\u6570\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u5c06\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\uff0c\u53ef\u4ee5\u5728\u6240\u6709\u51fd\u6570\u4e2d\u8bbf\u95ee\uff0c\u4f46\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u907f\u514d\u4e0d\u53ef\u9884\u77e5\u7684\u9519\u8bef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">global_var = 5  # \u5168\u5c40\u53d8\u91cf\n\ndef function_a():\n    print(global_var)\n\ndef function_b():\n    global global_var\n    global_var = 10  # \u4fee\u6539\u5168\u5c40\u53d8\u91cf\n\nfunction_a()  # \u8f93\u51fa: 5\nfunction_b()\nfunction_a()  # \u8f93\u51fa: 10\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u589e\u52a0\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u53d8\u91cf\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u5229\u7528\u95ed\u5305\u7b49\u65b9\u6cd5\u3002 \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1030028,"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\/1030006"}],"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=1030006"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030006\/revisions"}],"predecessor-version":[{"id":1030032,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1030006\/revisions\/1030032"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1030028"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1030006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1030006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1030006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}