{"id":2793,"date":"2025-03-14T23:13:29","date_gmt":"2025-03-14T15:13:29","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2793"},"modified":"2025-03-14T23:13:29","modified_gmt":"2025-03-14T15:13:29","slug":"python3-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%e5%92%8c%e4%bd%9c%e7%94%a8%e5%9f%9f%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python3-%e5%91%bd%e5%90%8d%e7%a9%ba%e9%97%b4%e5%92%8c%e4%bd%9c%e7%94%a8%e5%9f%9f%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python3 \u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u662f\u7ba1\u7406\u53d8\u91cf\u548c\u51fd\u6570\u7684\u5173\u952e\u6982\u5ff5\u3002\u7406\u89e3\u5b83\u4eec\u80fd\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u7ba1\u7406\u4ee3\u7801\u4e2d\u7684\u53d8\u91cf\u548c\u51fd\u6570\uff0c\u5e76\u907f\u514d\u53d8\u91cf\u540d\u51b2\u7a81\u3002\u4e0b\u9762\u662f\u5bf9 Python \u4e2d\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u7684\u8be6\u89e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u547d\u540d\u7a7a\u95f4\uff08Namespace\uff09<\/strong><\/h3>\n\n\n\n<p>\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u6620\u5c04\uff0c\u8868\u793a\u540d\u79f0\u5230\u5bf9\u8c61\u7684\u5173\u7cfb\u3002\u5728 Python \u4e2d\uff0c\u6bcf\u4e2a\u53d8\u91cf\u540d\uff08\u6216\u8005\u51fd\u6570\u540d\u3001\u7c7b\u540d\u7b49\uff09\u90fd\u5c5e\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u3002\u547d\u540d\u7a7a\u95f4\u662f Python \u7528\u6765\u7ba1\u7406\u6807\u8bc6\u7b26\uff08\u5982\u53d8\u91cf\u540d\u3001\u51fd\u6570\u540d\u7b49\uff09\u7684\u4e00\u79cd\u673a\u5236\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5168\u5c40\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5b58\u50a8\u5728\u7a0b\u5e8f\u7684\u9876\u5c42\uff08\u6a21\u5757\u5c42\u7ea7\uff09\u5b9a\u4e49\u7684\u53d8\u91cf\u3001\u51fd\u6570\u3001\u7c7b\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u5c40\u90e8\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5b58\u50a8\u5728\u51fd\u6570\u6216\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u7684\u53d8\u91cf\u548c\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5efa\u547d\u540d\u7a7a\u95f4<\/strong>\uff1aPython \u89e3\u91ca\u5668\u81ea\u5e26\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u5305\u542b\u4e86\u6240\u6709\u5185\u7f6e\u51fd\u6570\u548c\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f5c\u7528\u57df\uff08Scope\uff09<\/strong><\/h3>\n\n\n\n<p>\u4f5c\u7528\u57df\u662f\u6307\u53d8\u91cf\u6216\u6807\u8bc6\u7b26\u53ef\u4ee5\u88ab\u8bbf\u95ee\u7684\u533a\u57df\u6216\u4e0a\u4e0b\u6587\u3002\u5b83\u51b3\u5b9a\u4e86\u7a0b\u5e8f\u4e2d\u67d0\u4e2a\u6807\u8bc6\u7b26\u53ef\u4ee5\u5728\u54ea\u4e9b\u5730\u65b9\u88ab\u8bbf\u95ee\u3002\u6839\u636e\u4f5c\u7528\u57df\u7684\u8303\u56f4\u4e0d\u540c\uff0cPython \u4e2d\u7684\u4f5c\u7528\u57df\u5206\u4e3a\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c40\u90e8\u4f5c\u7528\u57df\uff08Local Scope\uff09<\/strong>\uff1a\u51fd\u6570\u6216\u65b9\u6cd5\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3001\u53c2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u5d4c\u5957\u4f5c\u7528\u57df\uff08Enclosing Scope\uff09<\/strong>\uff1a\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\uff08\u5d4c\u5957\u51fd\u6570\uff09\u4e2d\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u4f5c\u7528\u57df\uff08Global Scope\uff09<\/strong>\uff1a\u6a21\u5757\u7ea7\u522b\u7684\u4f5c\u7528\u57df\uff0c\u6240\u6709\u5728\u6a21\u5757\u9876\u5c42\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5efa\u4f5c\u7528\u57df\uff08Built-in Scope\uff09<\/strong>\uff1aPython \u89e3\u91ca\u5668\u7684\u5185\u5efa\u547d\u540d\u7a7a\u95f4\uff0c\u5305\u542b\u6240\u6709\u5185\u5efa\u51fd\u6570\u3001\u5f02\u5e38\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>Python \u901a\u8fc7 <strong>LEGB\uff08Local, Enclosing, Global, Built-in\uff09<\/strong> \u7684\u89c4\u5219\u6765\u67e5\u627e\u4e00\u4e2a\u53d8\u91cf\u6216\u6807\u8bc6\u7b26\u7684\u503c\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. LEGB \u89c4\u5219<\/strong><\/h3>\n\n\n\n<p>LEGB \u662f Python \u67e5\u627e\u53d8\u91cf\u65f6\u7684\u987a\u5e8f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Local (\u5c40\u90e8\u4f5c\u7528\u57df)<\/strong>\uff1a\u9996\u5148\u67e5\u627e\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>Enclosing (\u5d4c\u5957\u4f5c\u7528\u57df)<\/strong>\uff1a\u5982\u679c\u5728\u5c40\u90e8\u4f5c\u7528\u57df\u6ca1\u6709\u627e\u5230\uff0c\u63a5\u4e0b\u6765\u67e5\u627e\u5916\u5c42\u5d4c\u5957\u51fd\u6570\u7684\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>Global (\u5168\u5c40\u4f5c\u7528\u57df)<\/strong>\uff1a\u5982\u679c\u5728\u5c40\u90e8\u548c\u5d4c\u5957\u4f5c\u7528\u57df\u6ca1\u6709\u627e\u5230\uff0c\u63a5\u7740\u67e5\u627e\u6a21\u5757\u7ea7\u522b\u7684\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/li>\n\n\n\n<li><strong>Built-in (\u5185\u5efa\u4f5c\u7528\u57df)<\/strong>\uff1a\u5982\u679c\u5728\u5168\u5c40\u4f5c\u7528\u57df\u6ca1\u6709\u627e\u5230\uff0c\u6700\u540e\u67e5\u627e Python \u5185\u5efa\u4f5c\u7528\u57df\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u547d\u540d\u7a7a\u95f4\u7684\u521b\u5efa\u4e0e\u751f\u547d\u5468\u671f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5c40\u90e8\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5728\u51fd\u6570\u8c03\u7528\u65f6\u521b\u5efa\uff0c\u51fd\u6570\u6267\u884c\u7ed3\u675f\u540e\u9500\u6bc1\u3002<\/li>\n\n\n\n<li><strong>\u5168\u5c40\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5728\u6a21\u5757\u52a0\u8f7d\u65f6\u521b\u5efa\uff0c\u7a0b\u5e8f\u7ed3\u675f\u65f6\u9500\u6bc1\u3002<\/li>\n\n\n\n<li><strong>\u5185\u5efa\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u5728 Python \u542f\u52a8\u65f6\u521b\u5efa\uff0c\u5e76\u4e00\u76f4\u5b58\u5728\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u793a\u4f8b\u4e0e\u5e94\u7528<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 1\uff1a\u5c40\u90e8\u4f5c\u7528\u57df\u4e0e\u5168\u5c40\u4f5c\u7528\u57df<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef my_function():\n    x = 20  # \u5c40\u90e8\u53d8\u91cf\n    print(\"\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u7684 x:\", x)\n\nmy_function()\nprint(\"\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684 x:\", x)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u51fa<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  \u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u7684 x: 20\n  \u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684 x: 10<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u5728\u51fd\u6570 <code>my_function()<\/code> \u4e2d\uff0c<code>x<\/code> \u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u5b83\u548c\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684 <code>x<\/code> \u6ca1\u6709\u51b2\u7a81\u3002\u51fd\u6570\u5185\u90e8\u7684 <code>x<\/code> \u53ea\u5728\u5c40\u90e8\u4f5c\u7528\u57df\u4e2d\u6709\u6548\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 2\uff1a\u5d4c\u5957\u4f5c\u7528\u57df<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef outer_function():\n    x = 20  # \u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\n\n    def inner_function():\n        x = 30  # \u5185\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\n        print(\"\u5185\u90e8\u51fd\u6570\u4e2d\u7684 x:\", x)\n\n    inner_function()\n    print(\"\u5916\u90e8\u51fd\u6570\u4e2d\u7684 x:\", x)\n\nouter_function()\nprint(\"\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684 x:\", x)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u51fa<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  \u5185\u90e8\u51fd\u6570\u4e2d\u7684 x: 30\n  \u5916\u90e8\u51fd\u6570\u4e2d\u7684 x: 20\n  \u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684 x: 10<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a<code>inner_function()<\/code> \u67e5\u627e <code>x<\/code> \u65f6\u6309\u7167 LEGB \u89c4\u5219\uff0c\u9996\u5148\u627e\u5230\u81ea\u5df1\u7684\u5c40\u90e8\u53d8\u91cf <code>x<\/code>\u3002<code>outer_function()<\/code> \u4e2d\u7684 <code>x<\/code> \u662f <code>inner_function()<\/code> \u5916\u7684\u5d4c\u5957\u4f5c\u7528\u57df\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 3\uff1a\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\n\ndef my_function():\n    global x  # \u4f7f\u7528 global \u58f0\u660e\u53d8\u91cf\u4e3a\u5168\u5c40\n    x = 20\n\nmy_function()\nprint(x)  # \u8f93\u51fa 20<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u89e3\u91ca<\/strong>\uff1a\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. \u547d\u540d\u7a7a\u95f4\u7684\u67e5\u770b<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 Python \u7684 <code>globals()<\/code> \u548c <code>locals()<\/code> \u51fd\u6570\u6765\u67e5\u770b\u5f53\u524d\u7684\u5168\u5c40\u548c\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>globals()<\/code>\uff1a\u8fd4\u56de\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\u5b57\u5178\u3002<\/li>\n\n\n\n<li><code>locals()<\/code>\uff1a\u8fd4\u56de\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u7684\u5b57\u5178\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b 4\uff1a\u67e5\u770b\u547d\u540d\u7a7a\u95f4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef test():\n    y = 20  # \u5c40\u90e8\u53d8\u91cf\n    print(\"\u5c40\u90e8\u547d\u540d\u7a7a\u95f4:\", locals())\n    print(\"\u5168\u5c40\u547d\u540d\u7a7a\u95f4:\", globals())\n\ntest()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8f93\u51fa<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  \u5c40\u90e8\u547d\u540d\u7a7a\u95f4: {'y': 20}\n  \u5168\u5c40\u547d\u540d\u7a7a\u95f4: {'__name__': '__main__', '__doc__': None, ..., 'x': 10}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. \u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u7684\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u547d\u540d\u7a7a\u95f4<\/strong>\uff1a\u6307\u7684\u662f\u540d\u79f0\uff08\u5982\u53d8\u91cf\u540d\u3001\u51fd\u6570\u540d\uff09\u5230\u5bf9\u8c61\u7684\u6620\u5c04\u3002\u4e0d\u540c\u7684\u547d\u540d\u7a7a\u95f4\u7528\u4e8e\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u6807\u8bc6\u7b26\uff0c\u786e\u4fdd\u4e0d\u540c\u4f5c\u7528\u57df\u5185\u6807\u8bc6\u7b26\u4e0d\u4f1a\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><strong>\u4f5c\u7528\u57df<\/strong>\uff1a\u4f5c\u7528\u57df\u662f\u6307\u4e00\u4e2a\u6807\u8bc6\u7b26\u5728\u4ee3\u7801\u4e2d\u80fd\u88ab\u8bbf\u95ee\u7684\u533a\u57df\u3002Python \u6839\u636e LEGB \u89c4\u5219\u67e5\u627e\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>LEGB \u89c4\u5219<\/strong>\uff1aPython \u67e5\u627e\u6807\u8bc6\u7b26\u65f6\u4f1a\u6309\u7167\u5c40\u90e8\u4f5c\u7528\u57df\u3001\u5d4c\u5957\u4f5c\u7528\u57df\u3001\u5168\u5c40\u4f5c\u7528\u57df\u3001\u5185\u5efa\u4f5c\u7528\u57df\u7684\u987a\u5e8f\u67e5\u627e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u7406\u89e3\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u6709\u52a9\u4e8e\u907f\u514d\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df\u662f\u7ba1\u7406\u53d8\u91cf\u548c\u51fd\u6570\u7684\u5173\u952e\u6982\u5ff5\u3002\u7406\u89e3\u5b83\u4eec\u80fd\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u7ba1\u7406\u4ee3\u7801\u4e2d\u7684\u53d8\u91cf\u548c\u51fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2794,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2793"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2793\/revisions"}],"predecessor-version":[{"id":2795,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2793\/revisions\/2795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2794"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}