{"id":653,"date":"2026-03-08T10:11:00","date_gmt":"2026-03-08T02:11:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=653"},"modified":"2026-07-14T13:25:34","modified_gmt":"2026-07-14T05:25:34","slug":"python-%e4%bd%9c%e7%94%a8%e5%9f%9f%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/08\/python-%e4%bd%9c%e7%94%a8%e5%9f%9f%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a1%b9\/","title":{"rendered":"python \u4f5c\u7528\u57df\u6ce8\u610f\u4e8b\u9879"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u5728\u51fd\u6570\u5185\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5\u8d4b\u503c\u4f1a\u521b\u5efa\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u975e\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0<br>\u200b<br>def wrong_increment():<br> &nbsp; &nbsp;count += 1 &nbsp; &nbsp; &nbsp; &nbsp;# UnboundLocalError\uff01<br>\u200b<br># wrong_increment()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>count = 0<br>\u200b<br>def increment():<br> &nbsp; &nbsp;global count &nbsp; &nbsp; &nbsp;# \u58f0\u660e\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<br> &nbsp; &nbsp;count += 1<br> &nbsp; &nbsp;print(count)<br>\u200b<br>increment() &nbsp;# 1<br>increment() &nbsp;# 2<br>print(count) # 2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u95ed\u5305\u4f5c\u7528\u57df\uff08Enclosing\uff09<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5d4c\u5957\u51fd\u6570\u4e2d\uff0c\u5185\u5c42\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cf\uff1a\u53ef\u4ee5\u8bbf\u95ee\uff0c\u4f46\u662f\u4e0d\u80fd\u4fee\u6539\u5916\u5c42\u7684<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer():<br> &nbsp; &nbsp;x = \"outer\" &nbsp; &nbsp; &nbsp; # \u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;def inner():<br> &nbsp; &nbsp; &nbsp; &nbsp;print(x) &nbsp; &nbsp; &nbsp;# \u8bbf\u95ee\u5916\u5c42\u53d8\u91cf<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;inner()<br>\u200b<br>outer() &nbsp;# outer<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u62a5\u9519<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def outer():<br> &nbsp;  x = \"outer\" &nbsp; &nbsp; &nbsp; # \u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<br> &nbsp; &nbsp;<br> &nbsp;  def inner():<br> &nbsp;      x=x+\"inner\"<br> &nbsp; &nbsp; &nbsp;  print(x) &nbsp; &nbsp;  # \u8bbf\u95ee\u5916\u5c42\u53d8\u91cf<br> &nbsp; &nbsp;<br> &nbsp;  inner()<br>\u200b<br>outer()  # outer<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528 <code>nonlocal<\/code> \u5173\u952e\u5b57\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def make_counter():<br> &nbsp; &nbsp;count = 0<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;def increment():<br> &nbsp; &nbsp; &nbsp; &nbsp;nonlocal count &nbsp; # \u58f0\u660e\u4f7f\u7528\u5916\u5c42\u53d8\u91cf<br> &nbsp; &nbsp; &nbsp; &nbsp;count += 1<br> &nbsp; &nbsp; &nbsp; &nbsp;return count<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;return increment<br>\u200b<br>counter = make_counter()<br>print(counter()) &nbsp;# 1<br>print(counter()) &nbsp;# 2<br>print(counter()) &nbsp;# 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>nonlocal<\/code> vs <code>global<\/code>\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u952e\u5b57 \u4f5c\u7528 <code>global<\/code> \u58f0\u660e\u53d8\u91cf\u6765\u81ea<strong>\u5168\u5c40<\/strong>\u4f5c\u7528\u57df <code>nonlocal<\/code> \u58f0\u660e\u53d8\u91cf\u6765\u81ea<strong>\u5916\u5c42\u51fd\u6570<\/strong>\u4f5c\u7528\u57df<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e38\u89c1\u9519\u8bef<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u9519\u8bef 1\uff1a\u5728\u51fd\u6570\u5185\u540c\u65f6\u8bfb\u5199\u5168\u5c40\u53d8\u91cf<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10<br>\u200b<br>def demo():<br> &nbsp; &nbsp;print(x) &nbsp; &nbsp; &nbsp;# \u5148\u8bfb<br> &nbsp; &nbsp;x = 20 &nbsp; &nbsp; &nbsp; &nbsp;# \u518d\u5199 \u2192 \u7f16\u8bd1\u671f\u5c31\u5224\u5b9a x \u662f\u5c40\u90e8\u53d8\u91cf\uff01<br>\u200b<br># demo()  # UnboundLocalError<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u539f\u56e0\uff1a<\/strong> Python \u5728\u7f16\u8bd1\u51fd\u6570\u65f6\u5c31\u786e\u5b9a\u4e86\u53d8\u91cf\u4f5c\u7528\u57df\uff0c\u4e00\u65e6\u51fd\u6570\u5185\u6709\u8d4b\u503c\u8bed\u53e5\uff0c\u8be5\u53d8\u91cf\u5c31\u88ab\u89c6\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4fee\u6b63\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10<br>\u200b<br>def demo():<br> &nbsp; &nbsp;global x<br> &nbsp; &nbsp;print(x)<br> &nbsp; &nbsp;x = 20<br>\u200b<br>demo() &nbsp;# 10<br>print(x) &nbsp;# 20<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9519\u8bef 2\uff1a\u9ed8\u8ba4\u53c2\u6570\u7684\u9677\u9631<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_item(item, items=&#91;]):<br> &nbsp; &nbsp;items.append(item)<br> &nbsp; &nbsp;return items<br>\u200b<br>print(add_item(1)) &nbsp;# &#91;1]<br>print(add_item(2)) &nbsp;# &#91;1, 2] \u2014\u2014 \u610f\u5916\uff01\u5217\u8868\u88ab\u5171\u4eab\u4e86<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u6c42\u503c\uff0c\u53ea\u521b\u5efa\u4e00\u6b21\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4fee\u6b63\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_item(item, items=None):<br> &nbsp; &nbsp;if items is None:<br> &nbsp; &nbsp; &nbsp; &nbsp;items = &#91;]<br> &nbsp; &nbsp;items.append(item)<br> &nbsp; &nbsp;return items<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u95ed\u5305\u8ba1\u6570\u5668<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570 <code>make_multiplier(n)<\/code>\uff0c\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u3002\u8fd4\u56de\u7684\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u53c2\u6570 <code>x<\/code>\uff0c\u8fd4\u56de <code>n * x<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u6c42\u4f7f\u7528\u95ed\u5305\u5b9e\u73b0\uff0c\u4e0d\u8981\u4f7f\u7528 <code>global<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def make_multiplier(x):<br> &nbsp; &nbsp;def multiplier(n):<br> &nbsp; &nbsp; &nbsp; &nbsp;return x * n<br> &nbsp; &nbsp;return multiplier<br>\u200b<br>triple = make_multiplier(3)<br>print(triple(5)) &nbsp; # 15<br>print(triple(10)) &nbsp;# 30<br>\u200b<br>double = make_multiplier(2)<br>print(double(7)) &nbsp; # 14<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570 <code>create_account(initial_balance)<\/code>\uff0c\u8fd4\u56de\u4e24\u4e2a\u5b57\u5178\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>deposit(amount)<\/code>: \u5b58\u6b3e\uff0c\u8fd4\u56de\u65b0\u4f59\u989d<\/li>\n\n\n\n<li><code>withdraw(amount)<\/code>: \u53d6\u6b3e\uff0c\u4f59\u989d\u4e0d\u8db3\u8fd4\u56de <code>\"\u4f59\u989d\u4e0d\u8db3\"<\/code>\uff0c\u5426\u5219\u8fd4\u56de\u65b0\u4f59\u989d<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u6c42\u4f7f\u7528\u95ed\u5305\u4fdd\u5b58\u4f59\u989d\u72b6\u6001\uff0c\u4e0d\u8981\u66b4\u9732\u4f59\u989d\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def create_account(initial_balance):\n\n    def deposit(amount):\n        nonlocal initial_balance\n        initial_balance += amount\n        return initial_balance\n    def withdraw(amount):\n        nonlocal initial_balance\n        if amount > initial_balance:\n            return \"\u4f59\u989d\u4e0d\u8db3\"\n        else:\n            initial_balance -= amount\n            return initial_balance\n    return deposit, withdraw\ndeposit, withdraw = create_account(100)\nprint(deposit(50))    # 150\nprint(withdraw(30))   # 120\nprint(withdraw(200))  # \u4f59\u989d\u4e0d\u8db3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u51fd\u6570\u5185\u4fee\u6539\u5168\u5c40\u53d8\u91cf \u76f4\u63a5\u8d4b\u503c\u4f1a\u521b\u5efa\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u975e..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-653","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=653"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":654,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/653\/revisions\/654"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}