{"id":1123361,"date":"2025-01-08T19:31:35","date_gmt":"2025-01-08T11:31:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1123361.html"},"modified":"2025-01-08T19:31:37","modified_gmt":"2025-01-08T11:31: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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1123361.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084835\/4f5ae02c-33bc-484c-b144-b96cd1e973c8.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u901a\u8fc7\u76f4\u63a5\u8c03\u7528\u3001\u5728\u7c7b\u4e2d\u8c03\u7528\u3001\u901a\u8fc7\u6a21\u5757\u8c03\u7528\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u793a\u4f8b\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u7a0b\u5e8f\u65f6\uff0c\u51fd\u6570\u662f\u975e\u5e38\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\u3002\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7ec4\u7ec7\u4ee3\u7801\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002\u5728Python\u4e2d\uff0c\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u4f46\u8981\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u8c03\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>1.1 \u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u4f7f\u7528 <code>def<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u7684\u3002\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u53ea\u9700\u5728\u9700\u8981\u7684\u5730\u65b9\u8c03\u7528\u51fd\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>result = greet(&quot;Alice&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>greet<\/code> \u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7 <code>greet(&quot;Alice&quot;)<\/code> \u8c03\u7528\u5b83\u3002<strong>\u51fd\u6570\u7684\u76f4\u63a5\u8c03\u7528\u662f\u6700\u57fa\u672c\u3001\u6700\u5e38\u89c1\u7684\u8c03\u7528\u65b9\u5f0f<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u5e26\u53c2\u6570\u7684\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f20\u9012\u7ed9\u51fd\u6570\u5185\u90e8\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>sum_result = add(10, 5)<\/p>\n<p>print(sum_result)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>add<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570 <code>a<\/code> \u548c <code>b<\/code>\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u6211\u4eec\u5728\u8c03\u7528 <code>add<\/code> \u51fd\u6570\u65f6\u4f20\u9012\u4e86\u4e24\u4e2a\u6570\u503c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u5728\u7c7b\u4e2d\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>2.1 \u7c7b\u65b9\u6cd5\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\uff0c\u51fd\u6570\u901a\u5e38\u4f5c\u4e3a\u7c7b\u7684\u65b9\u6cd5\u5b9a\u4e49\u3002\u8c03\u7528\u7c7b\u7684\u65b9\u6cd5\u9700\u8981\u5148\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u901a\u8fc7\u5b9e\u4f8b\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\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>calc = Calculator()<\/p>\n<p>result = calc.add(10, 5)<\/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\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>Calculator<\/code> \u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e86 <code>add<\/code> \u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa <code>Calculator<\/code> \u7684\u5b9e\u4f8b <code>calc<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 <code>add<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u9759\u6001\u65b9\u6cd5\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u9759\u6001\u65b9\u6cd5\u4e0d\u9700\u8981\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u3002\u9759\u6001\u65b9\u6cd5\u4f7f\u7528 <code>@staticmethod<\/code> \u88c5\u9970\u5668\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MathUtils:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def multiply(a, b):<\/p>\n<p>        return a * b<\/p>\n<p>result = MathUtils.multiply(4, 5)<\/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 <code>MathUtils<\/code> \u7c7b\uff0c\u5e76\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e86 <code>multiply<\/code> \u9759\u6001\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7 <code>MathUtils.multiply<\/code> \u8c03\u7528\u8be5\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u901a\u8fc7\u6a21\u5757\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>3.1 \u5bfc\u5165\u6a21\u5757\u5e76\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>math_operations.py<\/code> \u7684\u6a21\u5757\uff0c\u5b9a\u4e49\u4e00\u4e9b\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># math_operations.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728\u53e6\u4e00\u4e2aPython\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u5e76\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import math_operations<\/p>\n<p>result_add = math_operations.add(10, 5)<\/p>\n<p>result_subtract = math_operations.subtract(10, 5)<\/p>\n<p>print(result_add)  # \u8f93\u51fa: 15<\/p>\n<p>print(result_subtract)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3.2 \u4ece\u6a21\u5757\u4e2d\u5bfc\u5165\u7279\u5b9a\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u67d0\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>from ... import ...<\/code> \u8bed\u6cd5\u5bfc\u5165\u7279\u5b9a\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from math_operations import add, subtract<\/p>\n<p>result_add = add(10, 5)<\/p>\n<p>result_subtract = subtract(10, 5)<\/p>\n<p>print(result_add)  # \u8f93\u51fa: 15<\/p>\n<p>print(result_subtract)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u907f\u514d\u5bfc\u5165\u4e0d\u9700\u8981\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u9012\u5f52\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>4.1 \u5b9a\u4e49\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\uff0c\u5373\u51fd\u6570\u5728\u5176\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u3002<\/p>\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<p>result = factorial(5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>factorial<\/code> \u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\u3002\u51fd\u6570\u5728\u5176\u5185\u90e8\u8c03\u7528\u81ea\u8eab\uff0c\u76f4\u5230\u6ee1\u8db3\u57fa\u51c6\u6761\u4ef6 <code>n == 0<\/code>\u3002<\/p>\n<\/p>\n<p><h3>4.2 \u9012\u5f52\u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u9700\u8981\u6ce8\u610f\u907f\u514d\u65e0\u9650\u9012\u5f52\u3002\u5982\u679c\u6ca1\u6709\u5408\u9002\u7684\u57fa\u51c6\u6761\u4ef6\uff0c\u9012\u5f52\u5c06\u65e0\u9650\u8fdb\u884c\uff0c\u5bfc\u81f4\u6808\u6ea2\u51fa\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def infinite_recursion():<\/p>\n<p>    return infinite_recursion()<\/p>\n<h2><strong>\u8c03\u7528 infinite_recursion() \u5c06\u5bfc\u81f4 RecursionError: maximum recursion depth exceeded<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u6709\u660e\u786e\u7684\u7ec8\u6b62\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u533f\u540d\u51fd\u6570\u7684\u8c03\u7528<\/h2>\n<\/p>\n<p><h3>5.1 Lambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>Python\u652f\u6301\u4f7f\u7528 <code>lambda<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u533f\u540d\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u51fd\u6570\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add = lambda a, b: a + b<\/p>\n<p>result = add(10, 5)<\/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\u6211\u4eec\u4f7f\u7528 <code>lambda<\/code> \u5b9a\u4e49\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570 <code>add<\/code>\uff0c\u5e76\u901a\u8fc7\u8c03\u7528 <code>add(10, 5)<\/code> \u83b7\u5f97\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>5.2 Lambda\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u4f8b\u5982\u5728\u5217\u8868\u6392\u5e8f\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [5, 2, 9, 1, 7]<\/p>\n<p>sorted_numbers = sorted(numbers, key=lambda x: x)<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [1, 2, 5, 7, 9]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>lambda x: x<\/code> \u4f5c\u4e3a\u6392\u5e8f\u952e\uff0c\u5c06\u5217\u8868 <code>numbers<\/code> \u6392\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u56de\u8c03\u51fd\u6570\u7684\u8c03\u7528<\/h2>\n<\/p>\n<p><h3>6.1 \u5b9a\u4e49\u548c\u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u56de\u8c03\u51fd\u6570\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u5728\u7279\u5b9a\u4e8b\u4ef6\u6216\u6761\u4ef6\u4e0b\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def process_data(data, callback):<\/p>\n<p>    # \u5904\u7406\u6570\u636e<\/p>\n<p>    processed_data = data.upper()<\/p>\n<p>    # \u8c03\u7528\u56de\u8c03\u51fd\u6570<\/p>\n<p>    callback(processed_data)<\/p>\n<p>def print_data(result):<\/p>\n<p>    print(f&quot;Processed Data: {result}&quot;)<\/p>\n<p>process_data(&quot;hello&quot;, print_data)  # \u8f93\u51fa: Processed Data: HELLO<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>print_data<\/code> \u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u4f20\u9012\u7ed9 <code>process_data<\/code> \u51fd\u6570\uff0c\u5e76\u5728 <code>process_data<\/code> \u51fd\u6570\u4e2d\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>6.2 \u9ad8\u9636\u51fd\u6570\u548c\u56de\u8c03<\/h3>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002\u56de\u8c03\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u9ad8\u9636\u51fd\u6570\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def apply_operation(a, b, operation):<\/p>\n<p>    return operation(a, b)<\/p>\n<p>def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>result = apply_operation(4, 5, multiply)<\/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<code>apply_operation<\/code> \u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u63a5\u53d7\u64cd\u4f5c\u51fd\u6570 <code>operation<\/code> \u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8c03\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8c03\u7528\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>7.1 \u5b9a\u4e49\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u65b0\u51fd\u6570\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u901a\u5e38\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\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<p>say_hello()<\/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\u5e76\u4f7f\u7528 <code>@my_decorator<\/code> \u8bed\u6cd5\u5c06\u5176\u5e94\u7528\u4e8e <code>say_hello<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>7.2 \u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u4e5f\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u4ee5\u4fbf\u5728\u88c5\u9970\u51fd\u6570\u65f6\u63d0\u4f9b\u66f4\u591a\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def repeat(num_times):<\/p>\n<p>    def decorator_repeat(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            for _ in range(num_times):<\/p>\n<p>                func(*args, kwargs)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator_repeat<\/p>\n<p>@repeat(num_times=3)<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668 <code>repeat<\/code>\uff0c\u5e76\u4f7f\u7528 <code>@repeat(num_times=3)<\/code> \u8bed\u6cd5\u5c06\u5176\u5e94\u7528\u4e8e <code>greet<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5f02\u6b65\u51fd\u6570\u7684\u8c03\u7528<\/h2>\n<\/p>\n<p><h3>8.1 \u5b9a\u4e49\u548c\u8c03\u7528\u5f02\u6b65\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u51fd\u6570\u4f7f\u7528 <code>async def<\/code> \u8bed\u6cd5\u5b9a\u4e49\uff0c\u5e76\u901a\u8fc7 <code>await<\/code> \u5173\u952e\u5b57\u8c03\u7528\u5176\u4ed6\u5f02\u6b65\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def say_hello():<\/p>\n<p>    print(&quot;Hello&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;World&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await say_hello()<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570 <code>say_hello<\/code>\uff0c\u5e76\u901a\u8fc7 <code>asyncio.run<\/code> \u8c03\u7528 <code>main<\/code> \u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>8.2 \u5f02\u6b65\u51fd\u6570\u4e2d\u7684\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u51fd\u6570\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>try<\/code>\/<code>except<\/code> \u8bed\u53e5\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def fetch_data():<\/p>\n<p>    try:<\/p>\n<p>        # \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>        raise ValueError(&quot;An error occurred!&quot;)<\/p>\n<p>    except ValueError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await fetch_data()<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728 <code>fetch_data<\/code> \u5f02\u6b65\u51fd\u6570\u4e2d\u5f15\u53d1\u4e00\u4e2a <code>ValueError<\/code>\uff0c\u5e76\u5728 <code>except<\/code> \u5757\u4e2d\u5904\u7406\u8be5\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h2>\u7ed3\u8bba<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86Python\u4e2d\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u76f4\u63a5\u8c03\u7528\u3001\u7c7b\u65b9\u6cd5\u8c03\u7528\u3001\u6a21\u5757\u8c03\u7528\u3001\u9012\u5f52\u8c03\u7528\u3001\u533f\u540d\u51fd\u6570\u8c03\u7528\u3001\u56de\u8c03\u51fd\u6570\u8c03\u7528\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8c03\u7528\u4ee5\u53ca\u5f02\u6b65\u51fd\u6570\u8c03\u7528\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u51fd\u6570\u8c03\u7528\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\u51fd\u6570\u5e76\u8c03\u7528\u5b83\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u548c\u53c2\u6570\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u4f7f\u7528\u51fd\u6570\u540d\u5e76\u4f20\u5165\u5fc5\u8981\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name):  \n    print(f&quot;Hello, {name}!&quot;)  \n  \ngreet(&quot;Alice&quot;)  # \u8f93\u51fa\uff1aHello, Alice!\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u66f4\u5177\u6a21\u5757\u5316\uff0c\u65b9\u4fbf\u7ef4\u62a4\u548c\u91cd\u7528\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f20\u9012\u53c2\u6570\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u6216\u9ed8\u8ba4\u53c2\u6570\u5411\u51fd\u6570\u4f20\u9012\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def add(a, b):  \n    return a + b  \n  \ndef calculate_sum():  \n    result = add(5, 3)  \n    print(f&quot;The sum is: {result}&quot;)  \n  \ncalculate_sum()  # \u8f93\u51fa\uff1aThe sum is: 8\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u6e05\u6670\u5730\u7ba1\u7406\u51fd\u6570\u4e4b\u95f4\u7684\u6570\u636e\u6d41\u52a8\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u8fd4\u56de\u503c\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\uff1f<\/strong><br \/>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u503c\uff0c\u8fd9\u4f7f\u5f97\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u7684\u8f93\u51fa\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u8f93\u5165\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def square(x):  \n    return x * x  \n  \ndef double_result(x):  \n    return 2 * square(x)  \n  \nresult = double_result(4)  \nprint(result)  # \u8f93\u51fa\uff1a32\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u903b\u8f91\u548c\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8c03\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff1a\u901a\u8fc7\u76f4\u63a5\u8c03\u7528\u3001\u5728\u7c7b\u4e2d\u8c03\u7528\u3001\u901a\u8fc7\u6a21\u5757\u8c03\u7528\u7b49\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0 [&hellip;]","protected":false},"author":3,"featured_media":1123367,"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\/1123361"}],"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=1123361"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123361\/revisions"}],"predecessor-version":[{"id":1123368,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1123361\/revisions\/1123368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1123367"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1123361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1123361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1123361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}