{"id":1048013,"date":"2024-12-31T13:47:22","date_gmt":"2024-12-31T05:47:22","guid":{"rendered":""},"modified":"2024-12-31T13:47:25","modified_gmt":"2024-12-31T05:47:25","slug":"python%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%a4%96%e9%83%a8%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1048013.html","title":{"rendered":"python\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/69971dd8-5ac8-4c25-9e17-7424c8fd7e97.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u51fd\u6570\u5982\u4f55\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c\" \/><\/p>\n<p><p> <strong>Python\u51fd\u6570\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528\u95ed\u5305\u3001\u5168\u5c40\u53d8\u91cf\u3001\u53c2\u6570\u4f20\u9012\u3001\u7c7b\u5c5e\u6027\u7b49\u65b9\u5f0f\uff0cPython\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u548c\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c\u3002<strong>\u95ed\u5305<\/strong>\u662f\u4e00\u79cd\u975e\u5e38\u5f3a\u5927\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8ba9\u5185\u90e8\u51fd\u6570\u8bb0\u4f4f\u5176\u5916\u90e8\u51fd\u6570\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u95ed\u5305\u7684\u4e00\u4e2a\u5178\u578b\u793a\u4f8b\u662f\uff1a\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u4f7f\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u5185\u90e8\u51fd\u6570\u4f9d\u7136\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\u548c\u89e3\u91ca\uff1a<\/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<\/p>\n<p>    return inner_function<\/p>\n<p>closure_example = outer_function(10)<\/p>\n<p>print(closure_example(5))  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>outer_function<\/code> \u662f\u5916\u90e8\u51fd\u6570\uff0c<code>inner_function<\/code> \u662f\u5185\u90e8\u51fd\u6570\u3002<code>inner_function<\/code> \u4f7f\u7528\u4e86\u5916\u90e8\u51fd\u6570 <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\u3002\u5373\u4f7f <code>outer_function<\/code> \u6267\u884c\u5b8c\u6bd5\uff0c<code>inner_function<\/code> \u4f9d\u7136\u8bb0\u4f4f\u4e86 <code>x<\/code> \u7684\u503c\uff0c\u5e76\u4e14\u80fd\u591f\u7ee7\u7eed\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5168\u5c40\u53d8\u91cf\u662f\u6307\u5728\u6240\u6709\u51fd\u6570\u4e4b\u5916\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u8981\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u3002<\/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 = x + 5<\/p>\n<p>    return x<\/p>\n<p>print(my_function())  # \u8f93\u51fa15<\/p>\n<p>print(x)  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code> \u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002\u5728 <code>my_function<\/code> \u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528 <code>global x<\/code> \u58f0\u660e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539 <code>x<\/code> \u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u53c2\u6570\u4f20\u9012<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u5c06\u5916\u90e8\u53d8\u91cf\u7684\u503c\u4f20\u9012\u7ed9\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u4f7f\u7528\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(10, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>add<\/code> \u51fd\u6570\u901a\u8fc7\u53c2\u6570 <code>a<\/code> \u548c <code>b<\/code> \u63a5\u6536\u5916\u90e8\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u5176\u5b9a\u4e49\u65f6\u6240\u5728\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u8fd9\u4e2a\u51fd\u6570\u5728\u5176\u5b9a\u4e49\u4f5c\u7528\u57df\u4e4b\u5916\u6267\u884c\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<\/p>\n<p>    return inner_function<\/p>\n<p>closure_example = outer_function(10)<\/p>\n<p>print(closure_example(5))  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code> \u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u8bb0\u4f4f\u4e86 <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\uff0c\u5373\u4f7f <code>outer_function<\/code> \u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7c7b\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u7c7b\u5c5e\u6027\u53ef\u4ee5\u4f5c\u4e3a\u5916\u90e8\u53d8\u91cf\u88ab\u51fd\u6570\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def add(self, x):<\/p>\n<p>        return self.value + x<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>print(obj.add(5))  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>MyClass<\/code> \u7684\u5b9e\u4f8b\u5bf9\u8c61 <code>obj<\/code> \u5177\u6709\u5c5e\u6027 <code>value<\/code>\uff0c<code>add<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8bbf\u95ee\u5e76\u4f7f\u7528\u8fd9\u4e2a\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>nonlocal<\/code>\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728\u5d4c\u5957\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 <code>nonlocal<\/code> \u5173\u952e\u5b57\u53ef\u4ee5\u8bbf\u95ee\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function():<\/p>\n<p>    x = 10<\/p>\n<p>    def inner_function():<\/p>\n<p>        nonlocal x<\/p>\n<p>        x = x + 5<\/p>\n<p>        return x<\/p>\n<p>    return inner_function()<\/p>\n<p>print(outer_function())  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code> \u4f7f\u7528 <code>nonlocal x<\/code> \u58f0\u660e\u6765\u8bbf\u95ee\u5e76\u4fee\u6539\u5916\u5c42\u51fd\u6570 <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u548c\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u548c\u5217\u8868\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff0c\u800c\u4e0d\u9700\u8981\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3]<\/p>\n<p>def modify_list(lst):<\/p>\n<p>    lst.append(4)<\/p>\n<p>modify_list(my_list)<\/p>\n<p>print(my_list)  # \u8f93\u51fa[1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>modify_list<\/code> \u51fd\u6570\u4fee\u6539\u4e86\u5916\u90e8\u5217\u8868 <code>my_list<\/code>\uff0c\u5e76\u5c06\u65b0\u7684\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u95ed\u5305\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6269\u5c55\u51fd\u6570\u7684\u529f\u80fd\u3002\u88c5\u9970\u5668\u53ef\u4ee5\u8bbf\u95ee\u5e76\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            print(f&quot;Using external variable x = {x}&quot;)<\/p>\n<p>            return func(*args, kwargs)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p>@outer_function(10)<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;Hello, World!&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>decorator<\/code> \u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5b83\u4f7f\u7528\u4e86\u5916\u90e8\u53d8\u91cf <code>x<\/code>\uff0c\u5e76\u6269\u5c55\u4e86 <code>my_function<\/code> \u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fdd\u5b58\u72b6\u6001\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u751f\u6210\u5668\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def generator():<\/p>\n<p>        for i in range(x):<\/p>\n<p>            yield i * 2<\/p>\n<p>    return generator<\/p>\n<p>gen = outer_function(5)<\/p>\n<p>for val in gen():<\/p>\n<p>    print(val)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>generator<\/code> \u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u5b83\u4f7f\u7528\u4e86\u5916\u90e8\u51fd\u6570 <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7684\u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u5728\u4f7f\u7528\u540e\u5f97\u5230\u6b63\u786e\u91ca\u653e\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyContextManager:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(f&quot;Entering with value = {self.value}&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        print(&quot;Exiting...&quot;)<\/p>\n<p>with MyContextManager(10) as manager:<\/p>\n<p>    print(f&quot;Using value = {manager.value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>MyContextManager<\/code> \u662f\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u5b83\u4f7f\u7528\u4e86\u5916\u90e8\u53d8\u91cf <code>value<\/code>\uff0c\u5e76\u5728\u8fdb\u5165\u548c\u9000\u51fa\u4e0a\u4e0b\u6587\u65f6\u6253\u5370\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u6a21\u5757\u7ea7\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u7ea7\u53d8\u91cf\u662f\u6307\u5728\u6a21\u5757\u9876\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6a21\u5757\u5185\u7684\u6240\u6709\u51fd\u6570\u548c\u7c7b\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">module_variable = 10<\/p>\n<p>def my_function():<\/p>\n<p>    return module_variable + 5<\/p>\n<p>print(my_function())  # \u8f93\u51fa15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>module_variable<\/code> \u662f\u4e00\u4e2a\u6a21\u5757\u7ea7\u53d8\u91cf\uff0c<code>my_function<\/code> \u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c\uff0c\u5305\u62ec\u5168\u5c40\u53d8\u91cf\u3001\u53c2\u6570\u4f20\u9012\u3001\u95ed\u5305\u3001\u7c7b\u5c5e\u6027\u3001<code>nonlocal<\/code> \u5173\u952e\u5b57\u3001\u5b57\u5178\u548c\u5217\u8868\u3001\u88c5\u9970\u5668\u3001\u751f\u6210\u5668\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u4ee5\u53ca\u6a21\u5757\u7ea7\u53d8\u91cf\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u51fd\u6570\u4e2d\u5f15\u7528\u5168\u5c40\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u5e0c\u671b\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u4f53\u5185\u5f15\u7528\u8be5\u53d8\u91cf\u3002\u7136\u800c\uff0c\u5982\u679c\u8981\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u53d8\u91cf\u662f\u5168\u5c40\u7684\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef my_function():\n    global x  # \u58f0\u660ex\u4e3a\u5168\u5c40\u53d8\u91cf\n    x += 5    # \u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\n    return x\n\nprint(my_function())  # \u8f93\u51fa15\n<\/code><\/pre>\n<p><strong>\u5728Python\u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u7684\u503c\uff1f<\/strong><br \/>\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002\u4e3a\u4e86\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u5b9a\u4e49\u5e76\u4f7f\u7528\u5b83\u4eec\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def my_function():\n    y = 20  # \u5c40\u90e8\u53d8\u91cf\n    return y\n\nprint(my_function())  # \u8f93\u51fa20\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u5916\u90e8\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9Python\u51fd\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u6307\u5b9a\u53c2\u6570\u6765\u5c06\u5916\u90e8\u53d8\u91cf\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5c06\u5916\u90e8\u53d8\u91cf\u7684\u503c\u4f5c\u4e3a\u5b9e\u53c2\u4f20\u9012\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def my_function(z):\n    return z * 2\n\nexternal_value = 15\nresult = my_function(external_value)  # \u5c06external_value\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\nprint(result)  # \u8f93\u51fa30\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u51fd\u6570\u4f7f\u7528\u5916\u90e8\u53d8\u91cf\u7684\u503c\uff1a\u901a\u8fc7\u4f7f\u7528\u95ed\u5305\u3001\u5168\u5c40\u53d8\u91cf\u3001\u53c2\u6570\u4f20\u9012\u3001\u7c7b\u5c5e\u6027\u7b49\u65b9\u5f0f\uff0cPython\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u548c\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1048023,"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\/1048013"}],"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=1048013"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048013\/revisions"}],"predecessor-version":[{"id":1048026,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1048013\/revisions\/1048026"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1048023"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1048013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1048013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1048013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}