{"id":1157367,"date":"2025-01-13T18:27:46","date_gmt":"2025-01-13T10:27:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157367.html"},"modified":"2025-01-13T18:27:48","modified_gmt":"2025-01-13T10:27:48","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%b7%b3%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1157367.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9e\u73b0\u8df3\u8f6c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195558\/d8ff1116-c8ef-4fad-a08f-04adbc7031bd.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9e\u73b0\u8df3\u8f6c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u8df3\u8f6c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u8bed\u53e5\uff08\u5982<code>break<\/code>\u3001<code>continue<\/code>\uff09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08\u5982<code>try<\/code>\u3001<code>except<\/code>\uff09\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u534f\u7a0b\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u548c\u5f02\u5e38\u5904\u7406\u673a\u5236\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><p><strong>\u51fd\u6570\u8c03\u7528<\/strong>\u662f\u5b9e\u73b0\u4ee3\u7801\u8df3\u8f6c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u3002\u5728Python\u4e2d\uff0c\u51fd\u6570\u8c03\u7528\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u91cd\u7528\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u6765\u5b9e\u73b0\u4ee3\u7801\u8df3\u8f6c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function_a():<\/p>\n<p>    print(&quot;Executing Function A&quot;)<\/p>\n<p>    function_b()<\/p>\n<p>def function_b():<\/p>\n<p>    print(&quot;Executing Function B&quot;)<\/p>\n<p>    function_c()<\/p>\n<p>def function_c():<\/p>\n<p>    print(&quot;Executing Function C&quot;)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>function_a()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>function_a<\/code>\u8c03\u7528<code>function_b<\/code>\uff0c<code>function_b<\/code>\u8c03\u7528<code>function_c<\/code>\uff0c\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u8fd9\u6837\u7684\u7ed3\u6784\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u4ee3\u7801\u7ec4\u7ec7\u5355\u5143\uff0c\u901a\u8fc7\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u903b\u8f91\u5206\u652f\u3002\u4f7f\u7528\u51fd\u6570\u8fdb\u884c\u8df3\u8f6c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51fd\u6570\u5b9a\u4e49\u548c\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u51fd\u6570\u4f53\u7f29\u8fdb\u8868\u793a\u51fd\u6570\u7684\u5185\u5bb9\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u4f7f\u7528\u51fd\u6570\u540d\u52a0\u4e0a\u62ec\u53f7\u5373\u53ef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    greet(&quot;Alice&quot;)<\/p>\n<p>    greet(&quot;Bob&quot;)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>greet<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u6253\u5370\u95ee\u5019\u8bed\u3002\u5728<code>main<\/code>\u51fd\u6570\u4e2d\uff0c\u5206\u522b\u8c03\u7528\u4e86<code>greet<\/code>\u51fd\u6570\u4e24\u6b21\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9012\u5f52\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u8c03\u7528\u81ea\u5df1\u7684\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\uff0c\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u3002\u9012\u5f52\u8c03\u7528\u901a\u8fc7\u8c03\u7528\u81ea\u8eab\u5b9e\u73b0\u8df3\u8f6c\uff0c\u76f4\u5230\u6ee1\u8db3\u7ec8\u6b62\u6761\u4ef6\u4e3a\u6b62\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u8ba1\u7b97\u9636\u4e58\u7684\u9012\u5f52\u51fd\u6570\u793a\u4f8b\uff1a<\/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>def main():<\/p>\n<p>    print(factorial(5))<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>factorial<\/code>\u51fd\u6570\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u81ea\u5df1\u6765\u8ba1\u7b97\u9636\u4e58\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u903b\u8f91\u5206\u652f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u8bed\u53e5\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u5faa\u73af\u63a7\u5236\u8bed\u53e5\uff08\u5982<code>break<\/code>\u3001<code>continue<\/code>\uff09\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u5b9e\u73b0\u8df3\u8f6c\u548c\u63a7\u5236\u6d41\u7a0b\u3002<code>break<\/code>\u7528\u4e8e\u63d0\u524d\u7ec8\u6b62\u5faa\u73af\uff0c<code>continue<\/code>\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\u5e76\u5f00\u59cb\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>break<\/code>\u8df3\u51fa\u5faa\u73af<\/h4>\n<\/p>\n<p><p><code>break<\/code>\u8bed\u53e5\u7528\u4e8e\u7acb\u5373\u7ec8\u6b62\u5f53\u524d\u5faa\u73af\uff0c\u5e76\u8df3\u51fa\u5faa\u73af\u4f53\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_first_even(numbers):<\/p>\n<p>    for number in numbers:<\/p>\n<p>        if number % 2 == 0:<\/p>\n<p>            print(f&quot;First even number: {number}&quot;)<\/p>\n<p>            break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;No even number found&quot;)<\/p>\n<p>def main():<\/p>\n<p>    numbers = [1, 3, 5, 7, 8, 9]<\/p>\n<p>    find_first_even(numbers)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u627e\u5230\u7b2c\u4e00\u4e2a\u5076\u6570\u65f6\uff0c\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u8df3\u51fa\u5faa\u73af\uff0c\u5e76\u6253\u5370\u8be5\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>continue<\/code>\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3<\/h4>\n<\/p>\n<p><p><code>continue<\/code>\u8bed\u53e5\u7528\u4e8e\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u5e76\u7ee7\u7eed\u4e0b\u4e00\u6b21\u8fed\u4ee3\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_non_negative(numbers):<\/p>\n<p>    for number in numbers:<\/p>\n<p>        if number &lt; 0:<\/p>\n<p>            continue<\/p>\n<p>        print(number)<\/p>\n<p>def main():<\/p>\n<p>    numbers = [1, -2, 3, -4, 5]<\/p>\n<p>    print_non_negative(numbers)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u9047\u5230\u8d1f\u6570\u65f6\uff0c\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u4e0d\u6253\u5370\u8d1f\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u673a\u5236\uff08\u5982<code>try<\/code>\u3001<code>except<\/code>\uff09\u53ef\u4ee5\u5728\u7a0b\u5e8f\u4e2d\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u9519\u8bef\u5904\u7406\u3002\u5f02\u5e38\u5904\u7406\u673a\u5236\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u8fd8\u53ef\u4ee5\u5728\u9047\u5230\u9519\u8bef\u65f6\u8df3\u8f6c\u5230\u7279\u5b9a\u7684\u4ee3\u7801\u5757\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>try<\/code>\u5757\u5305\u56f4\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\uff0c\u4f7f\u7528<code>except<\/code>\u5757\u5904\u7406\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        print(&quot;Error: Division by zero&quot;)<\/p>\n<p>        return None<\/p>\n<p>    else:<\/p>\n<p>        return result<\/p>\n<p>def main():<\/p>\n<p>    print(divide(10, 2))<\/p>\n<p>    print(divide(10, 0))<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u53d1\u751f\u9664\u96f6\u9519\u8bef\u65f6\uff0c\u4f7f\u7528<code>except<\/code>\u5757\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u6253\u5370\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u6355\u83b7\u5f02\u5e38\u5916\uff0c\u8fd8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5b9e\u73b0\u8df3\u8f6c\u548c\u9519\u8bef\u5904\u7406\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class NegativeNumberError(Exception):<\/p>\n<p>    pass<\/p>\n<p>def sqrt(number):<\/p>\n<p>    if number &lt; 0:<\/p>\n<p>        raise NegativeNumberError(&quot;Cannot calculate square root of a negative number&quot;)<\/p>\n<p>    return number  0.5<\/p>\n<p>def main():<\/p>\n<p>    try:<\/p>\n<p>        print(sqrt(9))<\/p>\n<p>        print(sqrt(-1))<\/p>\n<p>    except NegativeNumberError as e:<\/p>\n<p>        print(e)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5f53\u9047\u5230\u8d1f\u6570\u65f6\uff0c\u629b\u51fa\u81ea\u5b9a\u4e49\u5f02\u5e38<code>NegativeNumberError<\/code>\uff0c\u5e76\u5728<code>main<\/code>\u51fd\u6570\u4e2d\u6355\u83b7\u548c\u5904\u7406\u8be5\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u751f\u6210\u5668\u548c\u534f\u7a0b\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u548c\u534f\u7a0b\u662fPython\u4e2d\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u901a\u8fc7\u751f\u6210\u5668\u548c\u534f\u7a0b\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e2d\u5b9e\u73b0\u591a\u6b21\u8df3\u8f6c\u548c\u6682\u505c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u5904\u7406\u6216\u60f0\u6027\u6c42\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u6bcf\u6b21\u8c03\u7528<code>yield<\/code>\u90fd\u4f1a\u6682\u505c\u51fd\u6570\u7684\u6267\u884c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u751f\u6210\u5668\u5728\u4e0b\u4e00\u6b21\u8c03\u7528\u65f6\u4ece\u6682\u505c\u5904\u7ee7\u7eed\u6267\u884c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_up_to(max):<\/p>\n<p>    count = 1<\/p>\n<p>    while count &lt;= max:<\/p>\n<p>        yield count<\/p>\n<p>        count += 1<\/p>\n<p>def main():<\/p>\n<p>    for number in count_up_to(5):<\/p>\n<p>        print(number)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u51fd\u6570<code>count_up_to<\/code>\u6bcf\u6b21\u8c03\u7528<code>yield<\/code>\u65f6\u90fd\u4f1a\u6682\u505c\uff0c\u5e76\u8fd4\u56de\u5f53\u524d\u8ba1\u6570\u503c\uff0c\u76f4\u5230\u8fbe\u5230\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u534f\u7a0b<\/h4>\n<\/p>\n<p><p>\u534f\u7a0b\u662f\u4f7f\u7528<code>async def<\/code>\u5b9a\u4e49\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u6682\u505c\uff0c\u5e76\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u3002\u534f\u7a0b\u9002\u7528\u4e8e\u9700\u8981\u5f02\u6b65\u5904\u7406\u7684\u573a\u666f\uff0c\u5982\u7f51\u7edc\u8bf7\u6c42\u3001I\/O\u64cd\u4f5c\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u534f\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data():<\/p>\n<p>    print(&quot;Fetching data...&quot;)<\/p>\n<p>    await asyncio.sleep(2)<\/p>\n<p>    print(&quot;Data fetched&quot;)<\/p>\n<p>async def main():<\/p>\n<p>    await fetch_data()<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u534f\u7a0b\u51fd\u6570<code>fetch_data<\/code>\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c2\u79d2\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\u3002\u4f7f\u7528<code>await<\/code>\u5173\u952e\u5b57\u53ef\u4ee5\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\uff0c\u5b9e\u73b0\u8df3\u8f6c\u548c\u6682\u505c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff08OOP\uff09\u662fPython\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\uff0c\u901a\u8fc7\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u7ec4\u7ec7\u3002\u7c7b\u548c\u5bf9\u8c61\u4e0d\u4ec5\u53ef\u4ee5\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u65b9\u6cd5\u8c03\u7528\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u4f7f\u7528\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>class<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7c7b\uff0c\u7c7b\u4e2d\u5305\u542b\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa\u7c7b\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u8c03\u7528\u65b9\u6cd5\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        print(f&quot;Hello, {self.name}!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    greeter = Greeter(&quot;Alice&quot;)<\/p>\n<p>    greeter.greet()<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Greeter<\/code>\u7c7b\uff0c\u901a\u8fc7\u521b\u5efa<code>Greeter<\/code>\u5bf9\u8c61\u5e76\u8c03\u7528<code>greet<\/code>\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ee7\u627f\u548c\u591a\u6001<\/h4>\n<\/p>\n<p><p>\u7ee7\u627f\u548c\u591a\u6001\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u91cd\u8981\u6982\u5ff5\uff0c\u901a\u8fc7\u7ee7\u627f\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u7c7b\u5e76\u590d\u7528\u5df2\u6709\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1b\u901a\u8fc7\u591a\u6001\uff0c\u53ef\u4ee5\u4f7f\u7528\u7edf\u4e00\u7684\u63a5\u53e3\u8c03\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u7ee7\u627f\u548c\u591a\u6001\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Animal:<\/p>\n<p>    def speak(self):<\/p>\n<p>        pass<\/p>\n<p>class Dog(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(&quot;Woof!&quot;)<\/p>\n<p>class Cat(Animal):<\/p>\n<p>    def speak(self):<\/p>\n<p>        print(&quot;Meow!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    animals = [Dog(), Cat()]<\/p>\n<p>    for animal in animals:<\/p>\n<p>        animal.speak()<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a<code>Animal<\/code>\u57fa\u7c7b\u548c\u4e24\u4e2a\u6d3e\u751f\u7c7b<code>Dog<\/code>\u548c<code>Cat<\/code>\u3002\u901a\u8fc7\u521b\u5efa<code>Dog<\/code>\u548c<code>Cat<\/code>\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528<code>Animal<\/code>\u7c7b\u7684<code>speak<\/code>\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u591a\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u6a21\u5757\u548c\u5305\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u548c\u5305\u662fPython\u4e2d\u7ec4\u7ec7\u4ee3\u7801\u548c\u7ba1\u7406\u9879\u76ee\u7684\u91cd\u8981\u65b9\u5f0f\uff0c\u901a\u8fc7\u6a21\u5757\u548c\u5305\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u91cd\u7528\u3002\u6a21\u5757\u662f\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u548c\u5305\uff0c\u53ef\u4ee5\u8c03\u7528\u5176\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>import<\/code>\u5173\u952e\u5b57\u5bfc\u5165\u6a21\u5757\uff0c\u901a\u8fc7\u6a21\u5757\u540d\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u7c7b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># math_utils.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<h2><strong>main.py<\/strong><\/h2>\n<p>import math_utils<\/p>\n<p>def main():<\/p>\n<p>    print(math_utils.add(3, 2))<\/p>\n<p>    print(math_utils.subtract(3, 2))<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u5bfc\u5165<code>math_utils<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684<code>add<\/code>\u548c<code>subtract<\/code>\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u4f7f\u7528\u5305<\/h4>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u8fc7\u5728\u5305\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a<code>__init__.py<\/code>\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u76ee\u5f55\u6807\u8bc6\u4e3a\u5305\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u548c\u4f7f\u7528\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/__init__.py<\/p>\n<h2><strong>(empty file)<\/strong><\/h2>\n<h2><strong>my_package\/math_utils.py<\/strong><\/h2>\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<h2><strong>main.py<\/strong><\/h2>\n<p>from my_package import math_utils<\/p>\n<p>def main():<\/p>\n<p>    print(math_utils.add(3, 2))<\/p>\n<p>    print(math_utils.subtract(3, 2))<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7\u521b\u5efa<code>my_package<\/code>\u5305\uff0c\u5e76\u5728\u5305\u4e2d\u5b9a\u4e49<code>math_utils<\/code>\u6a21\u5757\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u7ec4\u7ec7\u548c\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u88c5\u9970\u5668\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u901a\u8fc7\u88c5\u9970\u5668\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u524d\u540e\u63d2\u5165\u989d\u5916\u7684\u4ee3\u7801\uff0c\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u589e\u5f3a\u51fd\u6570\u7684\u529f\u80fd\u3002\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u548c\u4f7f\u7528\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u88c5\u9970\u5668\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u88c5\u9970\u5668\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u4f7f\u7528\u88c5\u9970\u5668\u65f6\uff0c\u5728\u88ab\u88c5\u9970\u51fd\u6570\u524d\u4f7f\u7528<code>@<\/code>\u7b26\u53f7\u52a0\u4e0a\u88c5\u9970\u5668\u51fd\u6570\u540d\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u88c5\u9970\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Before function execution&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;After function execution&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    greet(&quot;Alice&quot;)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u88c5\u9970\u5668<code>my_decorator<\/code>\uff0c\u5728\u51fd\u6570\u6267\u884c\u524d\u540e\u63d2\u5165\u6253\u5370\u8bed\u53e5\u3002\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u589e\u5f3a\u4e86<code>greet<\/code>\u51fd\u6570\u7684\u529f\u80fd\uff0c\u5e76\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u94fe\u5f0f\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u88c5\u9970\u5668\u88c5\u9970\u540c\u4e00\u4e2a\u51fd\u6570\uff0c\u591a\u4e2a\u88c5\u9970\u5668\u4f1a\u6309\u7167\u4ece\u5185\u5230\u5916\u7684\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u94fe\u5f0f\u88c5\u9970\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator_one(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Decorator One - Before function execution&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;Decorator One - After function execution&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>def decorator_two(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Decorator Two - Before function execution&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;Decorator Two - After function execution&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@decorator_one<\/p>\n<p>@decorator_two<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>def main():<\/p>\n<p>    greet(&quot;Alice&quot;)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528\u4e86\u4e24\u4e2a\u88c5\u9970\u5668<code>decorator_one<\/code>\u548c<code>decorator_two<\/code>\u88c5\u9970<code>greet<\/code>\u51fd\u6570\uff0c\u901a\u8fc7\u94fe\u5f0f\u88c5\u9970\u5668\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u589e\u5f3a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662fPython\u4e2d\u7684\u4e00\u4e2a\u9ad8\u7ea7\u7279\u6027\uff0c\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5757\u6267\u884c\u524d\u540e\u63d2\u5165\u989d\u5916\u7684\u4ee3\u7801\uff0c\u5b9e\u73b0\u8d44\u6e90\u7684\u7ba1\u7406\u548c\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u901a\u5e38\u4e0e<code>with<\/code>\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u5e38\u89c1\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5305\u62ec\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u901a\u8fc7\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        content = file.read()<\/p>\n<p>    return content<\/p>\n<p>def main():<\/p>\n<p>    file_path = &#39;example.txt&#39;<\/p>\n<p>    content = read_file(file_path)<\/p>\n<p>    print(content)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u5b9e\u73b0\u4e86\u8d44\u6e90\u7684\u7ba1\u7406\u548c\u4ee3\u7801\u7684\u8df3\u8f6c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\u6765\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c<code>__enter__<\/code>\u65b9\u6cd5\u5728\u8fdb\u5165\u4e0a\u4e0b\u6587\u65f6\u6267\u884c\uff0c<code>__exit__<\/code>\u65b9\u6cd5\u5728\u79bb\u5f00\u4e0a\u4e0b\u6587\u65f6\u6267\u884c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u81ea\u5b9a\u4e49\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;Entering context&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_value, traceback):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>def main():<\/p>\n<p>    with MyContextManager():<\/p>\n<p>        print(&quot;Inside context&quot;)<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>MyContextManager<\/code>\uff0c\u901a\u8fc7\u5b9e\u73b0<code>__enter__<\/code>\u548c<code>__exit__<\/code>\u65b9\u6cd5\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u548c<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u8fdb\u884c\u8df3\u8f6c<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u662fPython\u4e2d\u7684\u5e76\u53d1\u7f16\u7a0b\u673a\u5236\uff0c\u901a\u8fc7\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u6267\u884c\u6d41\u4e2d\u5e76\u884c\u6267\u884c\u4ee3\u7801\uff0c\u5b9e\u73b0\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u5e76\u53d1\u5904\u7406\u3002\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u9002\u7528\u4e8e\u9700\u8981\u5e76\u884c\u5904\u7406\u7684\u573a\u666f\uff0c\u5982I\/O\u5bc6\u96c6\u578b\u4efb\u52a1\u548c\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff0c\u901a\u8fc7\u591a\u7ebf\u7a0b\u5b9e\u73b0\u5e76\u884c\u6267\u884c\u548c\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u591a\u7ebf\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def worker(name):<\/p>\n<p>    print(f&quot;Worker {name} starting&quot;)<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(f&quot;Worker {name} finished&quot;)<\/p>\n<p>def main():<\/p>\n<p>    threads = []<\/p>\n<p>    for i in range(3):<\/p>\n<p>        thread = threading.Thread(target=worker, args=(i,))<\/p>\n<p>        threads.append(thread)<\/p>\n<p>        thread.start()<\/p>\n<p>    for thread in threads:<\/p>\n<p>        thread.join()<\/p>\n<h2><strong>Start execution<\/strong><\/h2>\n<p>main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u521b\u5efa\u4e86\u4e09\u4e2a\u7ebf\u7a0b\uff0c\u5e76\u884c\u6267\u884c<code>worker<\/code>\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u8df3\u8f6c\u548c\u5e76\u53d1\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u591a\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>multiprocessing<\/code>\u6a21\u5757\u521b\u5efa\u548c\u7ba1\u7406\u8fdb\u7a0b\uff0c\u901a\u8fc7\u591a\u8fdb\u7a0b\u5b9e\u73b0\u5e76\u884c\u6267\u884c\u548c\u4ee3\u7801\u7684\u8df3\u8f6c\u3002\u4e0b\u9762\u662f\u4e00\u4e2a<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u4e0d\u540c\u51fd\u6570\u4e4b\u95f4\u7684\u8df3\u8f6c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u591a\u4e2a\u51fd\u6570\u5e76\u5229\u7528\u51fd\u6570\u8c03\u7528\u5b9e\u73b0\u8df3\u8f6c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u67d0\u4e9b\u6761\u4ef6\u8c03\u7528\u4e0d\u540c\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u903b\u8f91\u8df3\u8f6c\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u4ee3\u7801\u7ed3\u6784\u66f4\u52a0\u6e05\u6670\uff0c\u540c\u65f6\u589e\u5f3a\u4e86\u53ef\u7ef4\u62a4\u6027\u3002\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6216<code>switch<\/code>\uff08\u901a\u8fc7\u5b57\u5178\u6620\u5c04\uff09\u53ef\u4ee5\u7075\u6d3b\u51b3\u5b9a\u8df3\u8f6c\u76ee\u6807\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u5f02\u5e38\u5904\u7406\u5982\u4f55\u5f71\u54cd\u7a0b\u5e8f\u6d41\uff1f<\/strong><br \/>\u5f02\u5e38\u5904\u7406\u5728Python\u4e2d\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u7a0b\u5e8f\u7684\u8df3\u8f6c\u3002\u5f53\u7a0b\u5e8f\u9047\u5230\u9519\u8bef\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u5f02\u5e38\u5e76\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u7a0b\u5e8f\u5728\u906d\u9047\u9519\u8bef\u65f6\u8df3\u8f6c\u5230\u9519\u8bef\u5904\u7406\u903b\u8f91\uff0c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u5e76\u63d0\u4f9b\u53cb\u597d\u7684\u7528\u6237\u53cd\u9988\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5faa\u73af\u5b9e\u73b0\u6761\u4ef6\u8df3\u8f6c\uff1f<\/strong><br \/>\u5728\u5faa\u73af\u7ed3\u6784\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>break<\/code>\u548c<code>continue<\/code>\u8bed\u53e5\u6765\u5b9e\u73b0\u6761\u4ef6\u8df3\u8f6c\u3002<code>break<\/code>\u53ef\u4ee5\u7acb\u5373\u7ec8\u6b62\u5faa\u73af\uff0c\u800c<code>continue<\/code>\u5219\u8df3\u8fc7\u5f53\u524d\u8fed\u4ee3\uff0c\u76f4\u63a5\u8fdb\u5165\u4e0b\u4e00\u6b21\u5faa\u73af\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u6539\u53d8\u5faa\u73af\u6267\u884c\u6d41\u7684\u573a\u666f\uff0c\u4f7f\u5f97\u4ee3\u7801\u903b\u8f91\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u8df3\u8f6c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u51fd\u6570\u3001\u4f7f\u7528\u5faa\u73af\u63a7\u5236\u8bed\u53e5\uff08\u5982break\u3001continue\uff09\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1157370,"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\/1157367"}],"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=1157367"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157367\/revisions"}],"predecessor-version":[{"id":1157371,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1157367\/revisions\/1157371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1157370"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1157367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1157367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1157367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}