{"id":979725,"date":"2024-12-27T06:48:50","date_gmt":"2024-12-26T22:48:50","guid":{"rendered":""},"modified":"2024-12-27T06:48:52","modified_gmt":"2024-12-26T22:48:52","slug":"python%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e%e4%b8%8a%e5%b1%82%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/979725.html","title":{"rendered":"python\u5982\u4f55\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205424\/2cd9b083-ec69-4d11-ae22-ef298aa4edeb.webp\" alt=\"python\u5982\u4f55\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\" \/><\/p>\n<p><p> <strong>Python\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u3001\u5f02\u5e38\u5904\u7406\u673a\u5236\u548c\u6a21\u5757\u7ba1\u7406\u3002\u51fd\u6570\u8c03\u7528\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u4ee3\u7801\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\u5e76\u8fd4\u56de\u7ed3\u679c\uff0c\u8fbe\u5230\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u7684\u76ee\u7684\uff1b\u5f02\u5e38\u5904\u7406\u673a\u5236\u5219\u53ef\u4ee5\u901a\u8fc7\u6355\u6349\u5f02\u5e38\u5b9e\u73b0\u63a7\u5236\u6d41\u7684\u8df3\u8f6c\uff1b\u6a21\u5757\u7ba1\u7406\u5219\u901a\u8fc7\u7ed3\u6784\u5316\u4ee3\u7801\u5b9e\u73b0\u6a21\u5757\u5316\u8c03\u7528\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51fd\u6570\u8c03\u7528\u7684\u5b9e\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u662f\u4e00\u79cd\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u4ee3\u7801\u5c01\u88c5\u5355\u5143\uff0c\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u5c06\u4e00\u6bb5\u4ee3\u7801\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u901a\u8fc7\u8c03\u7528\u8be5\u51fd\u6570\u6765\u5b9e\u73b0\u4ee3\u7801\u7684\u590d\u7528\u548c\u8fd4\u56de\u3002\u51fd\u6570\u8c03\u7528\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u7ed3\u6784\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u5b9e\u73b0\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u8c03\u7528\u662fPython\u4e2d\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u548c\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u7684\u57fa\u672c\u65b9\u5f0f\u3002\u901a\u8fc7\u5c06\u4ee3\u7801\u5c01\u88c5\u5728\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u65f6\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u8fd4\u56de\u503c\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9\u4e0a\u5c42\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u901a\u8fc7<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u7684\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\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\u8981\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = add(2, 3)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2 \u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u662f\u5355\u4e2a\u503c\u3001\u591a\u4e2a\u503c\uff08\u5143\u7ec4\uff09\u6216\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u8fd4\u56de<code>None<\/code>\uff09\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f4d\u7f6e\u53c2\u6570\uff1a\u6309\u7167\u4f4d\u7f6e\u4f20\u9012\u53c2\u6570\u3002<\/li>\n<li>\u5173\u952e\u5b57\u53c2\u6570\uff1a\u901a\u8fc7\u53c2\u6570\u540d\u79f0\u4f20\u9012\u53c2\u6570\u3002<\/li>\n<li>\u9ed8\u8ba4\u53c2\u6570\uff1a\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u8c03\u7528\u65f6\u4e0d\u4f20\u9012\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/li>\n<li>\u53ef\u53d8\u53c2\u6570\uff1a\u901a\u8fc7<code>*args<\/code>\u548c<code>kwargs<\/code>\u6765\u5b9e\u73b0\uff0c\u5206\u522b\u7528\u4e8e\u63a5\u6536\u4e0d\u5b9a\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u3002<\/li>\n<\/ul>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message=&quot;Hello&quot;):<\/p>\n<p>    return f&quot;{message}, {name}!&quot;<\/p>\n<p>print(greet(&quot;Alice&quot;))           # \u8f93\u51fa: Hello, Alice!<\/p>\n<p>print(greet(&quot;Bob&quot;, &quot;Hi&quot;))       # \u8f93\u51fa: Hi, Bob!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u51fd\u6570\u7684\u4f5c\u7528\u57df<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u7684\u4f5c\u7528\u57df\u6307\u7684\u662f\u53d8\u91cf\u7684\u53ef\u89c1\u8303\u56f4\u3002\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u5728\u51fd\u6570\u5185\u90e8\u53ef\u89c1\uff1b\u800c\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def foo():<\/p>\n<p>    x = 5  # \u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(&quot;Inside foo:&quot;, x)<\/p>\n<p>foo()  # \u8f93\u51fa: Inside foo: 5<\/p>\n<p>print(&quot;Outside foo:&quot;, x)  # \u8f93\u51fa: Outside foo: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.4 \u8fd4\u56de\u503c\u7684\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u8fd4\u56de\u503c\u662f\u51fd\u6570\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u7528\u4e8e\u5c06\u51fd\u6570\u7684\u7ed3\u679c\u4f20\u9012\u7ed9\u8c03\u7528\u8005\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u662f\u4efb\u4f55Python\u5bf9\u8c61\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7ec4\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>print(square(4))  # \u8f93\u51fa: 16<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662fPython\u4e2d\u5904\u7406\u9519\u8bef\u548c\u5f02\u5e38\u60c5\u51b5\u7684\u91cd\u8981\u673a\u5236\u3002\u901a\u8fc7\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u6355\u6349\u5e76\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728\u5f02\u5e38\u5904\u7406\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se<\/code>\u8bed\u53e5\u4e3b\u52a8\u629b\u51fa\u5f02\u5e38\uff0c\u901a\u8fc7<code>try-except<\/code>\u7ed3\u6784\u6355\u6349\u5f02\u5e38\uff0c\u5e76\u901a\u8fc7<code>finally<\/code>\u8bed\u53e5\u6267\u884c\u6e05\u7406\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u6355\u6349\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u6355\u6349\u548c\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = 10 \/ 0<\/p>\n<p>except ZeroDivisionError:<\/p>\n<p>    print(&quot;Cannot divide by zero&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>10 \/ 0<\/code>\u4f1a\u5f15\u53d1<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u8be5\u5f02\u5e38\u5728<code>except<\/code>\u5757\u4e2d\u88ab\u6355\u6349\u5e76\u5904\u7406\uff0c\u4ece\u800c\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u629b\u51fa\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>raise<\/code>\u8bed\u53e5\u4e3b\u52a8\u629b\u51fa\u5f02\u5e38\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    if b == 0:<\/p>\n<p>        raise ValueError(&quot;Cannot divide by zero&quot;)<\/p>\n<p>    return a \/ b<\/p>\n<p>try:<\/p>\n<p>    result = divide(10, 0)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)  # \u8f93\u51fa: Cannot divide by zero<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3 \u5f02\u5e38\u7684\u4f20\u9012<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u8c03\u7528\u4e2d\uff0c\u5f02\u5e38\u53ef\u4ee5\u6cbf\u7740\u8c03\u7528\u6808\u5411\u4e0a\u4f20\u9012\uff0c\u76f4\u5230\u88ab\u6355\u6349\u6216\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    raise Exception(&quot;An error occurred&quot;)<\/p>\n<p>def func2():<\/p>\n<p>    try:<\/p>\n<p>        func1()<\/p>\n<p>    except Exception as e:<\/p>\n<p>        print(&quot;Caught an exception:&quot;, e)<\/p>\n<p>func2()  # \u8f93\u51fa: Caught an exception: An error occurred<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u6a21\u5757\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>Python\u7684\u6a21\u5757\u7ba1\u7406\u673a\u5236\u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u4ee3\u7801\u5212\u5206\u4e3a\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5bfc\u5165\u548c\u8c03\u7528\u8fd9\u4e9b\u6a21\u5757\u3002\u901a\u8fc7\u6a21\u5757\u7ba1\u7406\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u6a21\u5757\u5316\u548c\u590d\u7528\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa: 4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u6a21\u5757\u7684\u7ed3\u6784\u5316<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06\u4ee3\u7801\u5206\u4e3a\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4ee3\u7801\u7684\u7ed3\u6784\u5316\u548c\u6a21\u5757\u5316\u7ba1\u7406\uff1a<\/p>\n<\/p>\n<p><p>\u6a21\u5757<code>mymodule.py<\/code>\uff1a<\/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><\/code><\/pre>\n<\/p>\n<p><p>\u4e3b\u7a0b\u5e8f<code>main.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mymodule<\/p>\n<p>print(mymodule.greet(&quot;Alice&quot;))  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3 \u5305\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u901a\u8fc7<code>__init__.py<\/code>\u6587\u4ef6\u6765\u6807\u8bc6\u4e00\u4e2a\u76ee\u5f55\u4e3a\u5305\u3002\u53ef\u4ee5\u901a\u8fc7\u5305\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u591a\u4e2a\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><p>\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code>mypackage\/<\/p>\n<p>    __init__.py<\/p>\n<p>    module1.py<\/p>\n<p>    module2.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>module1.py<\/code>\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func1():<\/p>\n<p>    return &quot;Function 1&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>module2.py<\/code>\u4e2d\u5b9a\u4e49\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func2():<\/p>\n<p>    return &quot;Function 2&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mypackage import module1, module2<\/p>\n<p>print(module1.func1())  # \u8f93\u51fa: Function 1<\/p>\n<p>print(module2.func2())  # \u8f93\u51fa: Function 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662fPython\u4e2d\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u673a\u5236\uff0c\u901a\u8fc7<code>with<\/code>\u8bed\u53e5\u6765\u4f7f\u7528\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u901a\u5e38\u7528\u4e8e\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\u9700\u8981\u663e\u5f0f\u7ba1\u7406\u8d44\u6e90\u7684\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>with<\/code>\u8bed\u53e5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u65e0\u8bba\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\u662f\u5426\u51fa\u73b0\u5f02\u5e38\uff0c\u6587\u4ef6\u90fd\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h4>4.2 \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\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyContext:<\/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_val, exc_tb):<\/p>\n<p>        print(&quot;Exiting context&quot;)<\/p>\n<p>with MyContext():<\/p>\n<p>    print(&quot;Inside context&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Entering context<\/p>\n<p>Inside context<\/p>\n<p>Exiting context<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u662fPython\u4e2d\u7528\u4e8e\u8fed\u4ee3\u6570\u636e\u7ed3\u6784\u7684\u673a\u5236\u3002\u8fed\u4ee3\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u7edf\u4e00\u7684\u8fed\u4ee3\u63a5\u53e3\uff0c\u800c\u751f\u6210\u5668\u5219\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7<code>yield<\/code>\u5173\u952e\u5b57\u6765\u751f\u6210\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662f\u5b9e\u73b0\u4e86<code>__iter__<\/code>\u548c<code>__next__<\/code>\u65b9\u6cd5\u7684\u5bf9\u8c61\uff0c\u7528\u4e8e\u5728\u96c6\u5408\u4e0a\u8fed\u4ee3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyIterator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.index = 0<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.index &lt; len(self.data):<\/p>\n<p>            result = self.data[self.index]<\/p>\n<p>            self.index += 1<\/p>\n<p>            return result<\/p>\n<p>        else:<\/p>\n<p>            raise StopIteration<\/p>\n<p>my_iter = MyIterator([1, 2, 3])<\/p>\n<p>for item in my_iter:<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u751f\u6210\u5668<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u6570\u636e\u6d41\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    yield 1<\/p>\n<p>    yield 2<\/p>\n<p>    yield 3<\/p>\n<p>for item in my_generator():<\/p>\n<p>    print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u7b80\u5316\u590d\u6742\u7684\u8fed\u4ee3\u903b\u8f91\uff0c\u5e76\u63d0\u9ad8\u5185\u5b58\u6548\u7387\uff0c\u56e0\u4e3a\u751f\u6210\u5668\u5728\u6bcf\u6b21\u8fed\u4ee3\u65f6\u53ea\u751f\u6210\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u6240\u6709\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u673a\u5236\u6765\u5b9e\u73b0\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u3001\u5f02\u5e38\u5904\u7406\u3001\u6a21\u5757\u7ba1\u7406\uff0c\u8fd8\u662f\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u548c\u8fed\u4ee3\u5668\u751f\u6210\u5668\uff0c\u5f00\u53d1\u8005\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u573a\u666f\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u7ec4\u7ec7\u548c\u7ba1\u7406\u4ee3\u7801\u3002\u638c\u63e1\u8fd9\u4e9b\u673a\u5236\uff0c\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\uff0c\u8fd8\u80fd\u63d0\u9ad8\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u51fd\u6570\u8fd4\u56de\u4e0a\u5c42\u8c03\u7528\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u53ef\u4ee5\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u8be5\u51fd\u6570\u7684\u4e0a\u4e00\u5c42\u4ee3\u7801\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u4e0a\u5c42\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e2a\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c\uff0c\u53ef\u4ee5\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u63a5\u6536\u5e76\u6253\u5370\u8fd9\u4e2a\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u80fd\u5426\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u8fd4\u56de\u5230\u4e0a\u5c42\u4ee3\u7801\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u5141\u8bb8\u7a0b\u5e8f\u5728\u53d1\u751f\u9519\u8bef\u65f6\u5411\u4e0a\u4f20\u9012\u5f02\u5e38\u3002\u5728\u4f7f\u7528<code>try<\/code>\u548c<code>except<\/code>\u8bed\u53e5\u65f6\uff0c\u5f53\u9047\u5230\u5f02\u5e38\uff0c\u63a7\u5236\u4f1a\u8f6c\u79fb\u5230\u4e0a\u5c42\u8c03\u7528\u4ee3\u7801\u4e2d\u7684<code>except<\/code>\u5757\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u53ef\u4ee5\u8fd4\u56de\u5230\u4e0a\u5c42\u4ee3\u7801\uff0c\u8fd8\u80fd\u63d0\u4f9b\u9519\u8bef\u5904\u7406\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u9012\u5f52\u8fd4\u56de\u4e0a\u5c42\u8c03\u7528\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u5728\u6bcf\u6b21\u8c03\u7528\u81ea\u8eab\u65f6\u8fd4\u56de\u7ed3\u679c\u3002\u901a\u8fc7\u5728\u9012\u5f52\u7684\u6bcf\u4e00\u5c42\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\uff0c\u5c06\u8ba1\u7b97\u7ed3\u679c\u9010\u5c42\u8fd4\u56de\u5230\u6700\u521d\u7684\u8c03\u7528\u8005\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5206\u6b65\u8ba1\u7b97\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u8ba1\u7b97\u9636\u4e58\u6216\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3002\u5728\u8bbe\u8ba1\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u786e\u4fdd\u6709\u4e00\u4e2a\u660e\u786e\u7684\u7ec8\u6b62\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u9012\u5f52\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fd4\u56de\u4e0a\u5c42\u4ee3\u7801\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u51fd\u6570\u8c03\u7528\u3001\u5f02\u5e38\u5904\u7406\u673a\u5236\u548c\u6a21\u5757\u7ba1\u7406\u3002\u51fd\u6570\u8c03\u7528\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u4ee3\u7801\u5c01 [&hellip;]","protected":false},"author":3,"featured_media":979731,"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\/979725"}],"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=979725"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979725\/revisions"}],"predecessor-version":[{"id":979732,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979725\/revisions\/979732"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979731"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}