{"id":1100820,"date":"2025-01-08T15:43:32","date_gmt":"2025-01-08T07:43:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100820.html"},"modified":"2025-01-08T15:43:34","modified_gmt":"2025-01-08T07:43:34","slug":"python-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e8%87%aa%e5%ae%9a%e4%b9%89%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100820.html","title":{"rendered":"python \u51fd\u6570\u5982\u4f55\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063851\/1a64e97d-0ca5-403a-ba77-d75b5f366eba.webp\" alt=\"python \u51fd\u6570\u5982\u4f55\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u51fd\u6570\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u9012\u5f52\u8c03\u7528\u3001\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u8fdb\u884c\u8bf4\u660e\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u53ef\u4ee5\u4ece\u672c\u6587\u4e2d\u627e\u5230\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u76f4\u63a5\u8c03\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528\u662f\u6700\u7b80\u5355\u548c\u5e38\u89c1\u7684\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u5f0f\u3002\u4f60\u53ea\u9700\u8981\u5b9a\u4e49\u51fd\u6570\u5e76\u5728\u9700\u8981\u7684\u5730\u65b9\u8c03\u7528\u5b83\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>print(greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>greet<\/code> \u7684\u51fd\u6570\uff0c\u5e76\u5728\u540e\u7eed\u4ee3\u7801\u4e2d\u901a\u8fc7 <code>greet(&quot;Alice&quot;)<\/code> \u6765\u8c03\u7528\u5b83\uff0c\u4ece\u800c\u8f93\u51fa <code>Hello, Alice!<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u7528\u5173\u952e\u5b57 <code>def<\/code>\uff0c\u7136\u540e\u5728\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9\u901a\u8fc7\u51fd\u6570\u540d\u52a0\u62ec\u53f7\u7684\u5f62\u5f0f\u8fdb\u884c\u8c03\u7528\u3002\u62ec\u53f7\u5185\u53ef\u4ee5\u4f20\u9012\u53c2\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u9002\u7528\u4e8e\u51e0\u4e4e\u6240\u6709\u7684\u4f7f\u7528\u573a\u666f\uff0c\u975e\u5e38\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u9012\u5f52\u8c03\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u4e00\u79cd\u5f62\u5f0f\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u6216\u5177\u6709\u91cd\u590d\u6027\u8d28\u7684\u95ee\u9898\uff0c\u5982\u8ba1\u7b97\u9636\u4e58\u548c\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n-1)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>print(factorial(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>factorial<\/code> \u51fd\u6570\u8c03\u7528\u4e86\u81ea\u8eab\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\u3002\u901a\u8fc7 <code>factorial(5)<\/code>\uff0c\u6211\u4eec\u5f97\u5230\u4e86 <code>120<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u8c03\u7528\u662f\u4e00\u79cd\u5f3a\u5927\u4f46\u4e5f\u9700\u8981\u5c0f\u5fc3\u4f7f\u7528\u7684\u6280\u672f\u3002\u5b83\u80fd\u591f\u4f7f\u590d\u6742\u95ee\u9898\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u6e05\u6670\uff0c\u4f46\u4e5f\u5bb9\u6613\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff08\u5982\u6808\u6ea2\u51fa\uff09\u4ee5\u53ca\u590d\u6742\u6027\u589e\u52a0\u3002\u9012\u5f52\u51fd\u6570\u9700\u8981\u6709\u4e00\u4e2a\u57fa\u51c6\u6761\u4ef6\u6765\u7ec8\u6b62\u9012\u5f52\uff0c\u5426\u5219\u4f1a\u9677\u5165\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u901a\u8fc7\u56de\u8c03\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u5c06\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u6280\u672f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data, callback):<\/p>\n<p>    result = [callback(item) for item in data]<\/p>\n<p>    return result<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>data = [1, 2, 3, 4]<\/p>\n<p>print(process_data(data, square))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>process_data<\/code> \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u636e\u5217\u8868\u548c\u4e00\u4e2a\u56de\u8c03\u51fd\u6570 <code>callback<\/code>\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u6570\u636e\u9879\u5e94\u7528 <code>callback<\/code> \u51fd\u6570\u3002\u901a\u8fc7 <code>process_data(data, square)<\/code>\uff0c\u6211\u4eec\u5f97\u5230\u4e86 <code>[1, 4, 9, 16]<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u975e\u5e38\u6709\u7528\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u6216\u9700\u8981\u81ea\u5b9a\u4e49\u884c\u4e3a\u7684\u65f6\u5019\u3002\u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u4ee3\u7801\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u4f7f\u7528\u95ed\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u8fd4\u56de\u7684\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def make_multiplier(factor):<\/p>\n<p>    def multiplier(x):<\/p>\n<p>        return x * factor<\/p>\n<p>    return multiplier<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>double = make_multiplier(2)<\/p>\n<p>print(double(5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>make_multiplier<\/code> \u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a <code>multiplier<\/code> \u51fd\u6570\uff0c\u8fd9\u4e2a <code>multiplier<\/code> \u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee <code>make_multiplier<\/code> \u7684\u53c2\u6570 <code>factor<\/code>\u3002\u901a\u8fc7 <code>double(5)<\/code>\uff0c\u6211\u4eec\u5f97\u5230\u4e86 <code>10<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6280\u672f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u521b\u5efa\u51fd\u6570\u5bf9\u8c61\u5e76\u643a\u5e26\u72b6\u6001\u7684\u573a\u666f\u3002\u5b83\u4f7f\u5f97\u51fd\u6570\u66f4\u52a0\u7075\u6d3b\u548c\u52a8\u6001\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bb8\u591a\u9ad8\u7ea7\u7f16\u7a0b\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u901a\u8fc7\u7c7b\u548c\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7c7b\u548c\u65b9\u6cd5\u6765\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">class Calculator:<\/p>\n<p>    def add(self, a, b):<\/p>\n<p>        return a + b<\/p>\n<p>    def subtract(self, a, b):<\/p>\n<p>        return a - b<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>calc = Calculator()<\/p>\n<p>print(calc.add(3, 5))<\/p>\n<p>print(calc.subtract(10, 4))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>Calculator<\/code> \u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e86 <code>add<\/code> \u548c <code>subtract<\/code> \u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa <code>Calculator<\/code> \u7c7b\u7684\u5b9e\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u6838\u5fc3\u3002\u5b83\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002\u901a\u8fc7\u7c7b\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u76f8\u5173\u7684\u529f\u80fd\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u5355\u5143\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u51fd\u6570\uff0c\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u7684\u884c\u4e3a\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u68c0\u67e5\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello!&quot;)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>my_decorator<\/code> \u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a <code>wrapper<\/code> \u51fd\u6570\uff0c\u8fd9\u4e2a <code>wrapper<\/code> \u51fd\u6570\u5728\u8c03\u7528 <code>func<\/code> \u4e4b\u524d\u548c\u4e4b\u540e\u90fd\u6253\u5370\u4e86\u4e00\u4e9b\u4fe1\u606f\u3002\u901a\u8fc7 <code>@my_decorator<\/code> \u8bed\u6cd5\uff0c\u6211\u4eec\u5c06 <code>say_hello<\/code> \u51fd\u6570\u8fdb\u884c\u4e86\u88c5\u9970\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u548c\u7075\u6d3b\u7684\u529f\u80fd\u3002\u5b83\u4eec\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u53ef\u8bfb\uff0c\u540c\u65f6\u4e5f\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u6743\u9650\u9a8c\u8bc1\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002\u901a\u8fc7\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6269\u5c55\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e03\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>Lambda\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u51fd\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def apply_function(func, value):<\/p>\n<p>    return func(value)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>result = apply_function(lambda x: x + 1, 5)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>apply_function<\/code> \u51fd\u6570\uff0c\u5e76\u4f7f\u7528 <code>lambda<\/code> \u8868\u8fbe\u5f0f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002\u901a\u8fc7 <code>apply_function(lambda x: x + 1, 5)<\/code>\uff0c\u6211\u4eec\u5f97\u5230\u4e86 <code>6<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>Lambda\u8868\u8fbe\u5f0f\u662fPython\u4e2d\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u7684\u4e00\u79cd\u7b80\u6d01\u65b9\u5f0f\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u9700\u8981\u4e00\u4e2a\u7b80\u77ed\u51fd\u6570\u7684\u573a\u666f\uff0c\u5982\u6392\u5e8f\u3001\u8fc7\u6ee4\u7b49\u3002\u867d\u7136lambda\u8868\u8fbe\u5f0f\u529f\u80fd\u5f3a\u5927\uff0c\u4f46\u7531\u4e8e\u5176\u7b80\u6d01\u6027\uff0c\u4e5f\u5b58\u5728\u53ef\u8bfb\u6027\u5dee\u7684\u7f3a\u70b9\u3002\u56e0\u6b64\uff0c\u5728\u4ee3\u7801\u590d\u6742\u6027\u8f83\u9ad8\u65f6\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528\u6807\u51c6\u51fd\u6570\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><p><strong>\u516b\u3001\u901a\u8fc7\u751f\u6210\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u4f7f\u7528 <code>yield<\/code> \u5173\u952e\u5b57\u6765\u751f\u6210\u503c\u3002\u5b83\u4eec\u9002\u7528\u4e8e\u9700\u8981\u9010\u6b65\u751f\u6210\u5927\u91cf\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        yield n<\/p>\n<p>        n -= 1<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>for number in countdown(5):<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>countdown<\/code> \u51fd\u6570\u4f7f\u7528\u4e86 <code>yield<\/code> \u5173\u952e\u5b57\u6765\u9010\u6b65\u751f\u6210\u503c\u3002\u901a\u8fc7 <code>for number in countdown(5)<\/code>\uff0c\u6211\u4eec\u5f97\u5230\u4e86 <code>5, 4, 3, 2, 1<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5904\u7406\u9700\u8981\u9010\u6b65\u751f\u6210\u5927\u91cf\u6570\u636e\u7684\u573a\u666f\u3002\u5b83\u4eec\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u5185\u5b58\u6d88\u8017\uff0c\u5e76\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u751f\u6210\u5668\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u3001\u6d41\u6570\u636e\u7b49\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e5d\u3001\u901a\u8fc7\u6a21\u5757\u548c\u5305<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570\u5b9a\u4e49\u5728\u6a21\u5757\u548c\u5305\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u6765\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&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 my_module<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>print(my_module.greet(&quot;Alice&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06 <code>greet<\/code> \u51fd\u6570\u5b9a\u4e49\u5728 <code>my_module.py<\/code> \u6587\u4ef6\u4e2d\uff0c\u5e76\u5728 <code>main.py<\/code> \u6587\u4ef6\u4e2d\u901a\u8fc7 <code>import my_module<\/code> \u6765\u5bfc\u5165\u548c\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6a21\u5757\u548c\u5305\u6765\u7ec4\u7ec7\u4ee3\u7801\u662fPython\u9879\u76ee\u4e2d\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u5b83\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\uff0c\u540c\u65f6\u4e5f\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5408\u7406\u7684\u6a21\u5757\u5212\u5206\uff0c\u53ef\u4ee5\u4f7f\u9879\u76ee\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u5341\u3001\u901a\u8fc7\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6765\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def print_numbers():<\/p>\n<p>    for i in range(5):<\/p>\n<p>        print(i)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>thread = threading.Thread(target=print_numbers)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u6790\uff1a<\/strong> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>threading.Thread<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7ebf\u7a0b\uff0c\u5e76\u901a\u8fc7 <code>target<\/code> \u53c2\u6570\u6307\u5b9a\u8981\u8c03\u7528\u7684\u81ea\u5b9a\u4e49\u51fd\u6570 <code>print_numbers<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u7684\u6709\u6548\u624b\u6bb5\uff0c\u7279\u522b\u662f\u5728I\/O\u5bc6\u96c6\u578b\u548c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\u3002\u901a\u8fc7\u5e76\u53d1\u6267\u884c\uff0c\u53ef\u4ee5\u663e\u8457\u7f29\u77ed\u7a0b\u5e8f\u7684\u8fd0\u884c\u65f6\u95f4\u3002\u7136\u800c\uff0c\u5e76\u53d1\u7f16\u7a0b\u4e5f\u5e26\u6765\u4e86\u7ebf\u7a0b\u5b89\u5168\u548c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u95ee\u9898\uff0c\u9700\u8981\u5c0f\u5fc3\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u9012\u5f52\u8c03\u7528\u3001\u56de\u8c03\u51fd\u6570\u3001\u95ed\u5305\u3001\u7c7b\u548c\u65b9\u6cd5\u3001\u88c5\u9970\u5668\u3001lambda\u8868\u8fbe\u5f0f\u3001\u751f\u6210\u5668\u3001\u6a21\u5757\u548c\u5305\u3001\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u5404\u79cd\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u53ef\u4ee5\u4ece\u4e2d\u627e\u5230\u6709\u7528\u7684\u4fe1\u606f\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u901a\u5e38\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u63a5\u7740\u662f\u51fd\u6570\u540d\u79f0\u548c\u62ec\u53f7\u3002\u53ef\u4ee5\u5728\u62ec\u53f7\u5185\u6dfb\u52a0\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name):\n    print(f&quot;Hello, {name}!&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>name<\/code>\uff0c\u5e76\u5728\u8c03\u7528\u65f6\u6253\u5370\u51fa\u95ee\u5019\u8bed\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u8c03\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u5e76\u4f20\u9012\u53c2\u6570\uff1f<\/strong><br \/>\u8981\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ea\u9700\u4f7f\u7528\u51fd\u6570\u540d\u79f0\u5e76\u5728\u62ec\u53f7\u4e2d\u4f20\u9012\u6240\u9700\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">greet(&quot;Alice&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u786e\u4fdd\u4f20\u9012\u7684\u53c2\u6570\u7c7b\u578b\u4e0e\u51fd\u6570\u5b9a\u4e49\u4e2d\u7684\u53c2\u6570\u76f8\u5339\u914d\u3002<\/p>\n<p><strong>\u5982\u679c\u81ea\u5b9a\u4e49\u51fd\u6570\u9700\u8981\u8fd4\u56de\u503c\uff0c\u5e94\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u81ea\u5b9a\u4e49\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n<\/code><\/pre>\n<p>\u8c03\u7528\u6b64\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff1a  <\/p>\n<pre><code class=\"language-python\">result = add(5, 3)\nprint(result)  # \u8f93\u51fa8\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u51fd\u6570\u66f4\u5177\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6\u64cd\u4f5c\u4e2d\u4f7f\u7528\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u51fd\u6570\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u9012\u5f52\u8c03\u7528\u3001\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":1100827,"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\/1100820"}],"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=1100820"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100820\/revisions"}],"predecessor-version":[{"id":1100829,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100820\/revisions\/1100829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100827"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}