{"id":1146046,"date":"2025-01-08T23:13:35","date_gmt":"2025-01-08T15:13:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146046.html"},"modified":"2025-01-08T23:13:37","modified_gmt":"2025-01-08T15:13:37","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%8f%a6%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0%e7%9a%84%e8%bf%94%e5%9b%9e%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146046.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182312\/2e4b109d-731c-4b63-b8dc-d34a80affec4.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7684\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u5982\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u3001\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u3001\u5d4c\u5957\u8c03\u7528\u3001\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ea\u9700\u8981\u8c03\u7528\u76ee\u6807\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u5176\u4ed6\u65b9\u6cd5\u5982\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u3001\u5d4c\u5957\u8c03\u7528\u3001\u548c\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4e5f\u5404\u6709\u5176\u9002\u7528\u573a\u666f\u3002\u8be6\u7ec6\u63cf\u8ff0\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u7684\u65b9\u5f0f\uff1a\u4f60\u53ea\u9700\u8c03\u7528\u76ee\u6807\u51fd\u6570\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u662f\u6700\u5e38\u89c1\u548c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u51fd\u6570<code>func1<\/code>\uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u7136\u540e\u4f60\u60f3\u5728\u53e6\u4e00\u4e2a\u51fd\u6570<code>func2<\/code>\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u8fd4\u56de\u503c\u3002\u4f60\u53ea\u9700\u8c03\u7528<code>func1<\/code>\uff0c\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>def func2():<\/p>\n<p>    result = func1()<\/p>\n<p>    print(result)<\/p>\n<p>func2()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>func1<\/code>\u8fd4\u56de\u5b57\u7b26\u4e32&quot;Hello, World!&quot;\uff0c\u800c<code>func2<\/code>\u8c03\u7528<code>func1<\/code>\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u5b58\u50a8\u5728<code>result<\/code>\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u6253\u5370\u51fa\u6765\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u7b80\u5355\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u662f\u4e00\u79cd\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u7136\u540e\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u6765\u8c03\u7528\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u7528\u4e8e\u52a8\u6001\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return &quot;Hello, World!&quot;<\/p>\n<p>def func2():<\/p>\n<p>    return &quot;Goodbye, World!&quot;<\/p>\n<h2><strong>\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf<\/strong><\/h2>\n<p>function_variable = func1<\/p>\n<p>result = function_variable()<\/p>\n<p>print(result)  # \u8f93\u51fa &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u66f4\u6539\u53d8\u91cf\u6307\u5411\u7684\u51fd\u6570<\/strong><\/h2>\n<p>function_variable = func2<\/p>\n<p>result = function_variable()<\/p>\n<p>print(result)  # \u8f93\u51fa &quot;Goodbye, World!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>func1<\/code>\u8d4b\u503c\u7ed9<code>function_variable<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>function_variable<\/code>\u6765\u8c03\u7528<code>func1<\/code>\u3002\u968f\u540e\uff0c\u6211\u4eec\u5c06<code>function_variable<\/code>\u66f4\u6539\u4e3a\u6307\u5411<code>func2<\/code>\uff0c\u5e76\u518d\u6b21\u8c03\u7528\u5b83\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u5d4c\u5957\u8c03\u7528<\/h2>\n<\/p>\n<p><p>\u5d4c\u5957\u8c03\u7528\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u7684\u5185\u90e8\u76f4\u63a5\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u5c42\u6b21\u4e0a\u5904\u7406\u6570\u636e\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return 10<\/p>\n<p>def func2():<\/p>\n<p>    return func1() * 2<\/p>\n<p>def func3():<\/p>\n<p>    return func2() + 5<\/p>\n<p>result = func3()<\/p>\n<p>print(result)  # \u8f93\u51fa 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>func3<\/code>\u8c03\u7528<code>func2<\/code>\uff0c\u800c<code>func2<\/code>\u53c8\u8c03\u7528<code>func1<\/code>\u3002\u6700\u7ec8\uff0c<code>func3<\/code>\u8fd4\u56de\u4e00\u4e2a\u7ecf\u8fc7\u591a\u6b21\u5904\u7406\u7684\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b65\u5904\u7406\u6570\u636e\u7684\u590d\u6742\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11\uff0c\u8fd9\u610f\u5473\u7740\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u9ad8\u5ea6\u7075\u6d3b\u6027\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return 10<\/p>\n<p>def func2(func):<\/p>\n<p>    return func() * 3<\/p>\n<p>result = func2(func1)<\/p>\n<p>print(result)  # \u8f93\u51fa 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>func1<\/code>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>func2<\/code>\uff0c\u7136\u540e<code>func2<\/code>\u8c03\u7528<code>func1<\/code>\u5e76\u5bf9\u5176\u8fd4\u56de\u503c\u8fdb\u884c\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u4ee3\u7801\u9ad8\u5ea6\u53ef\u91cd\u7528\u548c\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u7ed3\u5408\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u4f7f\u7528\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u590d\u6742\u7684\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u5185\u90e8\u8fdb\u884c\u5d4c\u5957\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return 5<\/p>\n<p>def func2(func):<\/p>\n<p>    def inner_func():<\/p>\n<p>        return func() * 2<\/p>\n<p>    return inner_func<\/p>\n<p>nested_func = func2(func1)<\/p>\n<p>result = nested_func() + 3<\/p>\n<p>print(result)  # \u8f93\u51fa 13<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06<code>func1<\/code>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>func2<\/code>\uff0c<code>func2<\/code>\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570<code>inner_func<\/code>\uff0c\u5b83\u5728\u5185\u90e8\u8c03\u7528<code>func1<\/code>\u5e76\u5bf9\u5176\u8fd4\u56de\u503c\u8fdb\u884c\u5904\u7406\u3002\u6700\u7ec8\uff0c\u6211\u4eec\u8c03\u7528<code>nested_func<\/code>\u5e76\u5bf9\u5176\u7ed3\u679c\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u4e3a\u51fd\u6570\u6dfb\u52a0\u65b0\u7684\u884c\u4e3a\u3002\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        result = func()<\/p>\n<p>        return result * 2<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def func1():<\/p>\n<p>    return 10<\/p>\n<p>result = func1()<\/p>\n<p>print(result)  # \u8f93\u51fa 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u88c5\u9970\u5668<code>my_decorator<\/code>\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570<code>wrapper<\/code>\u3002<code>wrapper<\/code>\u5728\u5185\u90e8\u8c03\u7528\u4f20\u5165\u7684\u51fd\u6570\u5e76\u5bf9\u5176\u8fd4\u56de\u503c\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528<code>@my_decorator<\/code>\u8bed\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u88c5\u9970\u5668\u5e94\u7528\u5230<code>func1<\/code>\u4e0a\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u95ed\u5305<\/h2>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e26\u6709\u72b6\u6001\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p>closure = outer_func(5)<\/p>\n<p>result = closure(10)<\/p>\n<p>print(result)  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>outer_func<\/code>\u8fd4\u56de\u4e00\u4e2a\u5185\u90e8\u51fd\u6570<code>inner_func<\/code>\uff0c\u5e76\u4e14<code>inner_func<\/code>\u53ef\u4ee5\u8bbf\u95ee<code>outer_func<\/code>\u7684\u53d8\u91cf<code>x<\/code>\u3002\u901a\u8fc7\u8c03\u7528<code>closure<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u7ed9<code>inner_func<\/code>\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u5c01\u88c5\u51fd\u6570\u548c\u6570\u636e\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def method1(self):<\/p>\n<p>        return 10<\/p>\n<p>    def method2(self):<\/p>\n<p>        result = self.method1()<\/p>\n<p>        return result * 3<\/p>\n<p>obj = MyClass()<\/p>\n<p>result = obj.method2()<\/p>\n<p>print(result)  # \u8f93\u51fa 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7c7b<code>MyClass<\/code>\uff0c\u5b83\u5305\u542b\u4e24\u4e2a\u65b9\u6cd5<code>method1<\/code>\u548c<code>method2<\/code>\u3002<code>method2<\/code>\u8c03\u7528<code>method1<\/code>\u5e76\u5bf9\u5176\u8fd4\u56de\u503c\u8fdb\u884c\u5904\u7406\u3002\u901a\u8fc7\u521b\u5efa<code>MyClass<\/code>\u7684\u5b9e\u4f8b<code>obj<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528<code>method2<\/code>\u5e76\u83b7\u53d6\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528\u751f\u6210\u5668<\/h2>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u800c\u4e0d\u662f<code>return<\/code>\u6765\u8fd4\u56de\u503c\u3002\u751f\u6210\u5668\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u4e00\u7cfb\u5217\u503c\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8ba1\u7b97\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generator_func():<\/p>\n<p>    yield 10<\/p>\n<p>    yield 20<\/p>\n<p>    yield 30<\/p>\n<p>def consume_generator(gen):<\/p>\n<p>    result = 0<\/p>\n<p>    for value in gen:<\/p>\n<p>        result += value<\/p>\n<p>    return result<\/p>\n<p>gen = generator_func()<\/p>\n<p>result = consume_generator(gen)<\/p>\n<p>print(result)  # \u8f93\u51fa 60<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>generator_func<\/code>\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u5b83\u751f\u6210\u4e00\u7cfb\u5217\u503c\u3002<code>consume_generator<\/code>\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u751f\u6210\u5668\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5bf9\u751f\u6210\u5668\u751f\u6210\u7684\u503c\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/h2>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u5728\u53e6\u4e00\u4e2a\u51fd\u6570\u5b8c\u6210\u540e\u88ab\u8c03\u7528\u3002\u8fd9\u79cd\u65b9\u6cd5\u5e38\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\u548c\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def callback_func(result):<\/p>\n<p>    print(f&quot;Callback received: {result}&quot;)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_func(callback):<\/p>\n<p>    result = 10<\/p>\n<p>    callback(result)<\/p>\n<p>main_func(callback_func)  # \u8f93\u51fa &quot;Callback received: 10&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>callback_func<\/code>\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5b83\u5728<code>main_func<\/code>\u5b8c\u6210\u540e\u88ab\u8c03\u7528\u3002\u901a\u8fc7\u4f20\u9012<code>callback_func<\/code>\u4f5c\u4e3a\u53c2\u6570\u7ed9<code>main_func<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u5728<code>main_func<\/code>\u5185\u90e8\u8c03\u7528<code>callback_func<\/code>\u5e76\u4f20\u9012\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u4f7f\u7528\u533f\u540d\u51fd\u6570\uff08Lambda\uff09<\/h2>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\uff08Lambda\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5b9a\u4e49\u7b80\u5355\u7684\u3001\u77ed\u5c0f\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return 10<\/p>\n<p>lambda_func = lambda x: x * 2<\/p>\n<p>result = lambda_func(func1())<\/p>\n<p>print(result)  # \u8f93\u51fa 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570<code>lambda_func<\/code>\uff0c\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u5176\u4e24\u500d\u7684\u503c\u3002\u901a\u8fc7\u8c03\u7528<code>func1<\/code>\u5e76\u5c06\u5176\u8fd4\u56de\u503c\u4f20\u9012\u7ed9<code>lambda_func<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6700\u7ec8\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u3001\u5d4c\u5957\u8c03\u7528\u3001\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3001\u88c5\u9970\u5668\u3001\u95ed\u5305\u3001\u7c7b\u548c\u5bf9\u8c61\u3001\u751f\u6210\u5668\u3001\u56de\u8c03\u51fd\u6570\u548c\u533f\u540d\u51fd\u6570\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8981\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u540d\u5e76\u5728\u5176\u540e\u52a0\u4e0a\u62ec\u53f7\uff0c\u4f20\u5165\u5fc5\u8981\u7684\u53c2\u6570\u3002\u51fd\u6570\u6267\u884c\u540e\u4f1a\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u8c03\u7528\u5b83\uff0c\u50cf\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n\nresult = add(3, 5)\nprint(result)  # \u8f93\u51fa 8\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4ee5\u5b9e\u73b0\u590d\u6742\u8ba1\u7b97\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u5c06\u591a\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7ed3\u5408\u8d77\u6765\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u8ba1\u7b97\u3002\u901a\u8fc7\u8c03\u7528\u591a\u4e2a\u51fd\u6570\u5e76\u5c06\u5b83\u4eec\u7684\u8fd4\u56de\u503c\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u53ef\u4ee5\u8fdb\u884c\u94fe\u5f0f\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5148\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u8fdb\u884c\u5e73\u65b9\uff1a<\/p>\n<pre><code class=\"language-python\">def square(x):\n    return x * x\n\ndef add_and_square(a, b):\n    sum_result = add(a, b)\n    return square(sum_result)\n\nresult = add_and_square(2, 3)\nprint(result)  # \u8f93\u51fa 25\uff0c\u56e0\u4e3a (2 + 3) \u7684\u5e73\u65b9\u662f 25\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5904\u7406\u51fd\u6570\u8fd4\u56de\u503c\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff1f<\/strong><br \/>\u5f53\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5f02\u5e38\u60c5\u51b5\uff0c\u6bd4\u5982\u8fd4\u56de\u503c\u4e3a<code>None<\/code>\u6216\u629b\u51fa\u5f02\u5e38\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4f18\u96c5\u5730\u5904\u7406\u8fd9\u4e9b\u60c5\u51b5\u3002\u786e\u4fdd\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u68c0\u67e5\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\uff1a<\/p>\n<pre><code class=\"language-python\">def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_divide(a, b):\n    if b == 0:\n        return None  # \u8fd4\u56deNone\u8868\u793a\u65e0\u6cd5\u8fdb\u884c\u9664\u6cd5\n    return a \/ b\n\nresult = safe_divide(10, 0)\nif result is None:\n    print(&quot;\u65e0\u6cd5\u8fdb\u884c\u9664\u6cd5\uff0c\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)\nelse:\n    print(result)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7684\u65b9\u5f0f\u6709\u5f88\u591a\uff0c\u5982\u76f4\u63a5\u8c03\u7528\u51fd\u6570\u3001\u5c06\u51fd\u6570\u8d4b\u503c\u7ed9\u53d8\u91cf\u3001\u5d4c\u5957\u8c03\u7528\u3001\u4f7f\u7528\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1146060,"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\/1146046"}],"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=1146046"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146046\/revisions"}],"predecessor-version":[{"id":1146063,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146046\/revisions\/1146063"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146060"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}