{"id":1070903,"date":"2025-01-08T11:02:28","date_gmt":"2025-01-08T03:02:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1070903.html"},"modified":"2025-01-08T11:02:30","modified_gmt":"2025-01-08T03:02:30","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a9%e4%bb%a3%e7%a0%81%e4%b8%8d%e8%bf%90%e8%a1%8c%e7%a8%8b%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1070903.html","title":{"rendered":"python\u5982\u4f55\u8ba9\u4ee3\u7801\u4e0d\u8fd0\u884c\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101523\/6cc9d353-c41d-4d74-b907-2d4509822691.webp\" alt=\"python\u5982\u4f55\u8ba9\u4ee3\u7801\u4e0d\u8fd0\u884c\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u4ee3\u7801\u5728\u7a0b\u5e8f\u4e2d\u8fd0\u884c<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6ce8\u91ca\u3001\u6761\u4ef6\u5224\u65ad\u3001\u51fd\u6570\u5c01\u88c5\u548c\u6a21\u5757\u7684__name__\u5c5e\u6027\u3002\u901a\u8fc7\u5408\u7406\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<strong>\u4f7f\u7528\u6ce8\u91ca<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u6ce8\u91ca\u662f\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u9632\u6b62\u4ee3\u7801\u5728\u7a0b\u5e8f\u4e2d\u8fd0\u884c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e95\u53f7 (#) \u5728\u4ee3\u7801\u884c\u524d\u6dfb\u52a0\u6ce8\u91ca\u7b26\u53f7\uff0c\u8fd9\u6837\u8be5\u884c\u4ee3\u7801\u5c31\u4e0d\u4f1a\u88ab\u89e3\u91ca\u5668\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># print(&quot;This line will not run&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5176\u4ed6\u51e0\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u6ce8\u91ca\u662f\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u3002Python\u4e2d\u7684\u6ce8\u91ca\u4f7f\u7528\u4e95\u53f7\uff08#\uff09\u6765\u6807\u8bb0\u3002\u6ce8\u91ca\u53ef\u4ee5\u7528\u4e8e\u5355\u884c\u6216\u591a\u884c\uff0c\u4f46\u591a\u884c\u6ce8\u91ca\u901a\u5e38\u4f7f\u7528\u4e09\u4e2a\u8fde\u7eed\u7684\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5355\u884c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u5355\u884c\u6ce8\u91ca\u4f7f\u7528\u4e95\u53f7\uff08#\uff09\u6807\u8bb0\uff0c\u89e3\u91ca\u5668\u4f1a\u5ffd\u7565\u4e95\u53f7\u540e\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u591a\u884c\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u591a\u884c\u6ce8\u91ca\u4f7f\u7528\u4e09\u4e2a\u8fde\u7eed\u7684\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u5305\u56f4\u3002\u867d\u7136\u4e25\u683c\u6765\u8bf4\uff0c\u8fd9\u5e76\u4e0d\u662f\u6ce8\u91ca\uff0c\u800c\u662f\u591a\u884c\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u901a\u5e38\u88ab\u7528\u4f5c\u591a\u884c\u6ce8\u91ca\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&#39;&#39;&#39;<\/p>\n<p>\u8fd9\u4e9b\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e5f\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>&#39;&#39;&#39;<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6761\u4ef6\u5224\u65ad<\/h3>\n<\/p>\n<p><p>\u6761\u4ef6\u5224\u65ad\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u8fd0\u884c\u67d0\u6bb5\u4ee3\u7801\u3002\u5e38\u89c1\u7684\u6761\u4ef6\u5224\u65ad\u5305\u62ec<code>if<\/code>\u8bed\u53e5\u548c<code>while<\/code>\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h4>2.1 if\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u603b\u662f\u4e3a\u5047\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if False:<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 while\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>while<\/code>\u5faa\u73af\u6765\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u603b\u662f\u4e3a\u5047\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u5faa\u73af\u4f53\u5185\u7684\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while False:<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u51fd\u6570\u5c01\u88c5<\/h3>\n<\/p>\n<p><p>\u5c06\u4ee3\u7801\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u5e76\u4e14\u4e0d\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u53ef\u4ee5\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u9694\u79bb\u5927\u6bb5\u4ee3\u7801\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b9a\u4e49\u51fd\u6570\u4f46\u4e0d\u8c03\u7528<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5c06\u4ee3\u7801\u653e\u5728\u51fd\u6570\u4e2d\uff0c\u5e76\u4e14\u4e0d\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u8fd9\u6837\u8fd9\u4e9b\u4ee3\u7801\u5c31\u4e0d\u4f1a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u6761\u4ef6\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u51fd\u6570\u5c01\u88c5\uff0c\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>if False:<\/p>\n<p>    my_function()<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u5757\u7684__name__\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u5f53\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\uff0c<code>__name__<\/code>\u5c5e\u6027\u7684\u503c\u4e3a<code>&quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;<\/code>\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u8fd9\u4e00\u7279\u6027\uff0c\u786e\u4fdd\u67d0\u4e9b\u4ee3\u7801\u4ec5\u5728\u6a21\u5757\u88ab\u76f4\u63a5\u8fd0\u884c\u65f6\u624d\u6267\u884c\uff0c\u800c\u5728\u88ab\u5bfc\u5165\u65f6\u4e0d\u4f1a\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528__name__\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u4e2d\u4f7f\u7528<code>if __name__ == &quot;__main__&quot;:<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u9632\u6b62\u67d0\u4e9b\u4ee3\u7801\u5728\u6a21\u5757\u88ab\u5bfc\u5165\u65f6\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def main():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884c\u65f6\u624d\u4f1a\u6267\u884c&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2 \u591a\u4e2a\u6a21\u5757\u7684\u534f\u4f5c<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u6709\u591a\u4e2a\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u5bfc\u5165\u53e6\u4e00\u4e2a\u6a21\u5757\uff0c\u5e76\u786e\u4fdd\u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884c\u65f6\u624d\u6267\u884c\u67d0\u4e9b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># module1.py<\/p>\n<p>def main():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u53ea\u6709\u5728\u76f4\u63a5\u8fd0\u884cmodule1.py\u65f6\u624d\u4f1a\u6267\u884c&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<h2><strong>module2.py<\/strong><\/h2>\n<p>import module1<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c\uff0c\u65e0\u8bbamodule1.py\u662f\u5426\u88ab\u5bfc\u5165&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u9632\u6b62\u67d0\u4e9b\u4ee3\u7801\u8fd0\u884c\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u8c03\u8bd5\u548c\u8bb0\u5f55\u65e5\u5fd7\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8c03\u8bd5\u5668\u548c\u65e5\u5fd7\u8bb0\u5f55\u5e93\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u8c03\u8bd5\u5668\u5141\u8bb8\u4f60\u9010\u884c\u6267\u884c\u4ee3\u7801\uff0c\u5e76\u5728\u7279\u5b9a\u4f4d\u7f6e\u8bbe\u7f6e\u65ad\u70b9\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u9632\u6b62\u67d0\u4e9b\u4ee3\u7801\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def my_function():<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u5728\u65ad\u70b9\u5904\u6682\u505c&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u5e93\u5141\u8bb8\u4f60\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u5e76\u6839\u636e\u65e5\u5fd7\u7ea7\u522b\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u5e93\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f\uff0c\u5e76\u5728\u7279\u5b9a\u65e5\u5fd7\u7ea7\u522b\u4e0b\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.DEBUG)<\/p>\n<p>def my_function():<\/p>\n<p>    logging.debug(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8fd0\u884c&quot;)<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u63a7\u5236\u51fd\u6570\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u7b80\u5355\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u88c5\u9970\u5668\uff0c\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u6267\u884c\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def conditional_decorator(condition):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            if condition:<\/p>\n<p>                return func(*args, kwargs)<\/p>\n<p>            else:<\/p>\n<p>                print(&quot;\u51fd\u6570\u672a\u6267\u884c&quot;)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p>@conditional_decorator(False)<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2 \u590d\u6742\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f8b\u5982\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u3001\u7f13\u5b58\u7ed3\u679c\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u88c5\u9970\u5668\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def logging_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;\u8c03\u7528\u51fd\u6570 {func.__name__}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;\u51fd\u6570 {func.__name__} \u6267\u884c\u5b8c\u6bd5&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@logging_decorator<\/p>\n<p>def my_function():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>my_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u5757\u7684\u8fdb\u5165\u548c\u9000\u51fa\u65f6\u6267\u884c\u7279\u5b9a\u64cd\u4f5c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u7b80\u5355\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u51b3\u5b9a\u662f\u5426\u6267\u884c\u4ee3\u7801\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ConditionalContextManager:<\/p>\n<p>    def __init__(self, condition):<\/p>\n<p>        self.condition = condition<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        if not self.condition:<\/p>\n<p>            print(&quot;\u4ee3\u7801\u5757\u672a\u6267\u884c&quot;)<\/p>\n<p>        return self.condition<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        pass<\/p>\n<p>with ConditionalContextManager(False) as condition:<\/p>\n<p>    if condition:<\/p>\n<p>        print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2 \u590d\u6742\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u4f8b\u5982<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u3001\u4e8b\u52a1\u5904\u7406\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class LoggingContextManager:<\/p>\n<p>    def __enter__(self):<\/p>\n<p>        print(&quot;\u8fdb\u5165\u4ee3\u7801\u5757&quot;)<\/p>\n<p>        return self<\/p>\n<p>    def __exit__(self, exc_type, exc_val, exc_tb):<\/p>\n<p>        print(&quot;\u9000\u51fa\u4ee3\u7801\u5757&quot;)<\/p>\n<p>with LoggingContextManager():<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u65ad\u8a00<\/h3>\n<\/p>\n<p><p>\u65ad\u8a00\u662f\u4e00\u79cd\u8c03\u8bd5\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u68c0\u67e5\u70b9\uff0c\u786e\u4fdd\u7279\u5b9a\u6761\u4ef6\u4e3a\u771f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u65ad\u8a00\u6765\u9632\u6b62\u4ee3\u7801\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u7b80\u5355\u65ad\u8a00<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>assert<\/code>\u8bed\u53e5\u68c0\u67e5\u6761\u4ef6\uff0c\u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u7a0b\u5e8f\u4f1a\u629b\u51fa<code>AssertionError<\/code>\uff0c\u9632\u6b62\u540e\u7eed\u4ee3\u7801\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">assert False, &quot;\u6761\u4ef6\u4e3a\u5047\uff0c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;<\/p>\n<p>print(&quot;\u8fd9\u884c\u4ee3\u7801\u4e0d\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>8.2 \u590d\u6742\u65ad\u8a00<\/h4>\n<\/p>\n<p><p>\u65ad\u8a00\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u68c0\u67e5\uff0c\u4f8b\u5982\u7c7b\u578b\u68c0\u67e5\u3001\u8303\u56f4\u68c0\u67e5\u7b49\u3002\u901a\u8fc7\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u548c\u65ad\u8a00\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function(x):<\/p>\n<p>    assert isinstance(x, int), &quot;\u53c2\u6570\u7c7b\u578b\u5fc5\u987b\u4e3a\u6574\u6570&quot;<\/p>\n<p>    assert x &gt; 0, &quot;\u53c2\u6570\u5fc5\u987b\u4e3a\u6b63\u6570&quot;<\/p>\n<p>    print(&quot;\u8fd9\u884c\u4ee3\u7801\u4f1a\u8fd0\u884c&quot;)<\/p>\n<p>my_function(1)<\/p>\n<h2><strong>my_function(&quot;a&quot;)  # \u8fd9\u884c\u4ee3\u7801\u4f1a\u89e6\u53d1\u65ad\u8a00\u9519\u8bef<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u662f\u4e00\u79cd\u9694\u79bb\u7684Python\u73af\u5883\uff0c\u53ef\u4ee5\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5e93\u548c\u4f9d\u8d56\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\uff0c\u4f8b\u5982\u9632\u6b62\u67d0\u4e9b\u5e93\u7684\u4ee3\u7801\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h4>9.1 \u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff0c\u5e76\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # \u6fc0\u6d3b\u865a\u62df\u73af\u5883<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.2 \u5b89\u88c5\u4f9d\u8d56<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684\u5e93\u548c\u4f9d\u8d56\uff0c\u5e76\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install requests==2.25.1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>9.3 \u8fd0\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u865a\u62df\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u4e0d\u4f1a\u56e0\u4e3a\u5e93\u7248\u672c\u4e0d\u517c\u5bb9\u800c\u8fd0\u884c\u5931\u8d25\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>response = requests.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<p>print(response.status_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\uff0c\u9632\u6b62\u67d0\u4e9b\u4ee3\u7801\u5728\u7279\u5b9a\u6761\u4ef6\u4e0b\u8fd0\u884c\u3002\u8fd9\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u8fd8\u53ef\u4ee5\u5728\u5f00\u53d1\u548c\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6682\u505c\u4ee3\u7801\u6267\u884c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u6765\u6682\u505c\u7a0b\u5e8f\u7684\u6267\u884c\u3002\u901a\u8fc7\u5f15\u5165<code>time<\/code>\u6a21\u5757\u5e76\u6307\u5b9a\u6682\u505c\u7684\u65f6\u95f4\uff08\u4ee5\u79d2\u4e3a\u5355\u4f4d\uff09\uff0c\u4ee3\u7801\u5c06\u6682\u65f6\u505c\u6b62\u8fd0\u884c\u3002\u4f8b\u5982\uff0c<code>time.sleep(5)<\/code>\u4f1a\u8ba9\u7a0b\u5e8f\u6682\u505c5\u79d2\u949f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u8df3\u8fc7\u67d0\u4e9b\u4ee3\u7801\uff1f<\/strong><br \/>\u901a\u8fc7<code>if<\/code>\u8bed\u53e5\u53ef\u4ee5\u63a7\u5236\u4ee3\u7801\u7684\u6267\u884c\u6d41\u3002\u82e5\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>continue<\/code>\u8bed\u53e5\u8df3\u8fc7\u5f53\u524d\u5faa\u73af\u4e2d\u7684\u5269\u4f59\u4ee3\u7801\uff0c\u6216\u4f7f\u7528<code>return<\/code>\u4ece\u51fd\u6570\u4e2d\u8fd4\u56de\uff0c\u4ee5\u7ec8\u6b62\u51fd\u6570\u7684\u6267\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u54ea\u4e9b\u4ee3\u7801\u7247\u6bb5\u88ab\u6267\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u673a\u5236\u4f7f\u7a0b\u5e8f\u4e0d\u6267\u884c\u7279\u5b9a\u4ee3\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u7ed3\u6784\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u4e0d\u4f1a\u7ee7\u7eed\u6267\u884c\u7279\u5b9a\u7684\u4ee3\u7801\u6bb5\u3002\u8fd9\u6837\uff0c\u53ef\u4ee5\u6839\u636e\u9519\u8bef\u7c7b\u578b\u6765\u9009\u62e9\u6027\u5730\u8df3\u8fc7\u67d0\u4e9b\u4ee3\u7801\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u7a0b\u5e8f\u7684\u6574\u4f53\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9632\u6b62\u4ee3\u7801\u5728\u7a0b\u5e8f\u4e2d\u8fd0\u884c\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u6ce8\u91ca\u3001\u6761\u4ef6\u5224\u65ad\u3001\u51fd\u6570\u5c01\u88c5\u548c\u6a21\u5757\u7684__na [&hellip;]","protected":false},"author":3,"featured_media":1070911,"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\/1070903"}],"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=1070903"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070903\/revisions"}],"predecessor-version":[{"id":1070912,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1070903\/revisions\/1070912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1070911"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1070903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1070903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1070903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}