{"id":107401,"date":"2026-09-19T16:56:46","date_gmt":"2026-09-19T08:56:46","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/107401.html"},"modified":"2026-09-19T16:56:46","modified_gmt":"2026-09-19T08:56:46","slug":"%e5%a4%a7%e6%a8%a1%e5%9e%8b%e5%ad%a6%e4%b9%a0%e4%b9%8bpython%e5%9f%ba%e7%a1%80-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/107401.html","title":{"rendered":"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570"},"content":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3 Python \u51fd\u6570\u7684\u5b8c\u6574\u77e5\u8bc6\u4f53\u7cfb&#xff0c;\u6db5\u76d6\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5\u3001\u5404\u7c7b\u53c2\u6570\u7c7b\u578b&#xff08;\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff09;\u53ca\u5176\u5b9a\u4e49\u987a\u5e8f\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f5c\u7528\u57df&#xff08;\u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf\u3001global \u4e0e nonlocal \u7684\u5bf9\u6bd4\u3001LEGB \u67e5\u627e\u89c4\u5219&#xff09;\u3001\u5d4c\u5957\u51fd\u6570\u4ee5\u53ca lambda \u8868\u8fbe\u5f0f\u3002\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u5747\u914d\u6709\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u6613\u9519\u70b9\u603b\u7ed3&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 Python \u51fd\u6570\u7684\u6838\u5fc3\u7528\u6cd5\u4e0e\u5e38\u89c1\u9677\u9631\u3002<\/p>\n<\/p>\n<h3>\u56db\u3001\u51fd\u6570<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"3632\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919085642-6aae4e4aad608.png\" width=\"1397\" \/><\/p>\n<h3>4.1 \u8bed\u6cd5<\/h3>\n<p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u793a\u4f8b\u6f14\u793a\u51fd\u6570\u5b9a\u4e49\u3001\u6587\u6863\u5b57\u7b26\u4e32\u548c\u8c03\u7528\u7684\u57fa\u672c\u8bed\u6cd5&#xff1a;<\/p>\n<ul>\n<li>def \u51fd\u6570\u540d(\u53c2\u6570\u5217\u8868):\n<ul>\n<li>\u51fd\u6570\u4f53<\/li>\n<li>return &#8230;<\/li>\n<\/ul>\n<\/li>\n<li>\u6587\u6863\u5b57\u7b26\u4e32 docstring<\/li>\n<li>\u51fd\u6570\u8c03\u7528&#xff1a;\u51fd\u6570(\u5b9e\u53c2\u5217\u8868)<\/li>\n<\/ul>\n<p>def greet(name):<br \/>\n    &#034;&#034;&#034;Hello world&#034;&#034;&#034;<br \/>\n    return &#034;Hello world &#034; &#043; name<br \/>\n# \u51fd\u6570\u8c03\u7528<br \/>\nprint(greet(&#034;zs&#034;))  # \u8f93\u51fa&#xff1a;Hello, zs<\/p>\n<ul><\/ul>\n<h3>4.2 \u53c2\u6570\u7c7b\u578b<\/h3>\n<h4>4.2.1 \u4f4d\u7f6e\u53c2\u6570<\/h4>\n<p>\u6700\u57fa\u7840&#xff0c;\u6309\u4f4d\u7f6e\u4e00\u4e00\u5bf9\u5e94&#xff0c;\u5fc5\u987b\u4f20\u53c2<\/p>\n<p>\u6570\u91cf\u4e0d\u5339\u914d\u4f1a\u62a5TypeError<\/p>\n<p>\u4e0b\u9762\u6f14\u793a\u4f4d\u7f6e\u53c2\u6570\u7684\u57fa\u672c\u7528\u6cd5&#xff0c;\u4ee5\u53ca\u5e38\u89c1\u7684\u6613\u9519\u70b9&#xff1a;<\/p>\n<p>def add(a, b):<br \/>\n    return a &#043; b<br \/>\n# \u6b63\u786e&#xff1a;\u6309\u4f4d\u7f6e\u4e00\u4e00\u5bf9\u5e94<br \/>\nprint(add(1, 2))      # \u8f93\u51fa&#xff1a;3<br \/>\n# \u5b9e\u53c2\u6570\u91cf\u5c11\u4e8e\u5f62\u53c2&#xff0c;\u62a5 TypeError<br \/>\nprint(add(1))       # TypeError: add() missing 1 required positional argument: &#039;b&#039;<br \/>\n# \u5b9e\u53c2\u6570\u91cf\u591a\u4e8e\u5f62\u53c2&#xff0c;\u62a5 TypeError<br \/>\nprint(add(1, 2, 3)) # TypeError: add() takes 2 positional arguments but 3 were given<\/p>\n<h4>4.2.2 \u5173\u952e\u5b57\u53c2\u6570<\/h4>\n<p>\u4e0b\u9762\u6f14\u793a\u5173\u952e\u5b57\u53c2\u6570\u7684\u7528\u6cd5&#xff0c;\u4ee5\u53ca\u5e38\u89c1\u7684\u6613\u9519\u70b9&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8c03\u7528\u65f6\u901a\u8fc7&#xff1a;\u5f62\u53c2\u540d &#061; &#034;\u5b57\u9762\u91cf \/ \u53d8\u91cf&#034;<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u65f6\u4f4d\u7f6e\u5b9e\u53c2\u5fc5\u987b\u4f4d\u4e8e\u5173\u952e\u5b57\u5b9e\u53c2\u4e4b\u524d&#xff1b;\u540c\u662f\u5173\u952e\u5b57\u53c2\u6570\u4e4b\u95f4\u53ef\u4efb\u610f\u987a\u5e8f<\/p>\n<\/li>\n<li>\n<p>\u540c\u4e00\u4e2a\u53c2\u6570\u4e0d\u80fd\u540c\u65f6\u7528\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u4f20\u4e24\u6b21&#xff0c;\u5426\u5219\u4f1a\u62a5<\/p>\n<\/li>\n<\/ul>\n<p>def introduce(name, age, city):<br \/>\n    return f&#034;{name}&#xff0c;{age} \u5c81&#xff0c;\u6765\u81ea {city}&#034;<br \/>\n# \u6b63\u786e&#xff1a;\u5168\u90e8\u4f7f\u7528\u5173\u952e\u5b57\u4f20\u53c2&#xff0c;\u987a\u5e8f\u53ef\u4efb\u610f<br \/>\nprint(introduce(city&#061;&#034;\u5317\u4eac&#034;, name&#061;&#034;Alice&#034;, age&#061;25))<br \/>\n# \u8f93\u51fa&#xff1a;Alice&#xff0c;25 \u5c81&#xff0c;\u6765\u81ea \u5317\u4eac<br \/>\n# \u6b63\u786e&#xff1a;\u4f4d\u7f6e\u53c2\u6570\u5728\u524d&#xff0c;\u5173\u952e\u5b57\u53c2\u6570\u5728\u540e<br \/>\nprint(introduce(&#034;Bob&#034;, age&#061;30, city&#061;&#034;\u4e0a\u6d77&#034;))<br \/>\n# \u8f93\u51fa&#xff1a;Bob&#xff0c;30 \u5c81&#xff0c;\u6765\u81ea \u4e0a\u6d77<br \/>\n# \u5173\u952e\u5b57\u53c2\u6570\u4f4d\u4e8e\u4f4d\u7f6e\u53c2\u6570\u4e4b\u524d&#xff0c;\u62a5 SyntaxError<br \/>\nprint(introduce(name&#061;&#034;Tom&#034;, 28, &#034;\u5e7f\u5dde&#034;))<br \/>\n# SyntaxError: positional argument follows keyword argument<br \/>\n# \u540c\u4e00\u4e2a\u53c2\u6570\u540c\u65f6\u7528\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u4f20\u4e24\u6b21&#xff0c;\u62a5 TypeError<br \/>\nprint(introduce(&#034;Tom&#034;, name&#061;&#034;Tom&#034;, age&#061;28, city&#061;&#034;\u5e7f\u5dde&#034;))<br \/>\n# TypeError: introduce() got multiple values for argument &#039;name&#039;<\/p>\n<h4>4.2.3 \u9ed8\u8ba4\u53c2\u6570<\/h4>\n<p>def \u51fd\u6570\u540d(name&#061;default): \u51fd\u6570\u4f53<\/p>\n<p>def greet(name&#061;&#034;world&#034;):<br \/>\n    print(f&#034;hello&#xff0c;{name}&#034;)<br \/>\ngreet()          # \u4e0d\u4f20&#xff0c;\u7528\u9ed8\u8ba4\u503c \u2192 hello&#xff0c;world<br \/>\ngreet(&#034;zs&#034;)    # \u4f20\u4e86&#xff0c;\u7528\u4f20\u5165\u503c \u2192 hello&#xff0c;zs<\/p>\n<p>\u9ed8\u8ba4\u53c2\u6570\u5fc5\u987b\u653e\u5728\u4f4d\u7f6e\u53c2\u6570\u4e4b\u540e<\/p>\n<p>def greet(name, msg&#061;&#034;\u4f60\u597d&#034;):<br \/>\n    print(f&#034;{msg}&#xff0c;{name}&#034;)<br \/>\ngreet(&#034;\u5c0f\u660e&#034;)            # \u4f60\u597d&#xff0c;\u5c0f\u660e<br \/>\ngreet(&#034;\u5c0f\u660e&#034;, &#034;\u65e9\u4e0a\u597d&#034;)   # \u65e9\u4e0a\u597d&#xff0c;\u5c0f\u660e<br \/>\ndef greet(msg&#061;&#034;\u4f60\u597d&#034;, name):   # \u8bed\u6cd5\u9519\u8bef&#xff01;<br \/>\n    print(f&#034;{msg}&#xff0c;{name}&#034;)    # SyntaxError: non-default argument follows default argument<\/p>\n<p>\u7528 None \u4f5c\u4e3a\u9ed8\u8ba4\u503c&#xff0c;\u51fd\u6570\u4f53\u5185\u5224\u65ad\u5e76\u521b\u5efa\u65b0\u5bf9\u8c61<\/p>\n<p>def add_item(item, lst&#061;None):<br \/>\n    if lst is None:        # \u5224\u65ad\u662f\u5426\u4f20\u4e86\u53c2\u6570<br \/>\n        lst &#061; []           # \u6ca1\u4f20&#xff0c;\u5c31\u521b\u5efa\u65b0\u5bf9\u8c61<br \/>\n    lst.append(item)<br \/>\n    return lst<br \/>\nprint(add_item(&#034;a&#034;))   # [&#039;a&#039;]<br \/>\nprint(add_item(&#034;b&#034;))   # [&#039;b&#039;]   \u2190 \u6b63\u786e&#xff01;\u6bcf\u6b21\u90fd\u662f\u65b0\u5217\u8868<br \/>\nprint(add_item(&#034;c&#034;))   # [&#039;c&#039;]<\/p>\n<p>\u9ed8\u8ba4\u53c2\u6570\u662f\u5b9a\u4e49\u51fd\u6570\u65f6\u4e00\u6b21\u6027\u521b\u5efa<\/p>\n<ul>\n<li>\n<p>\u673a\u5236&#xff1a;\u9ed8\u8ba4\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6&#xff08;def \u8bed\u53e5\u6267\u884c\u65f6&#xff09;\u6c42\u503c\u4e00\u6b21&#xff0c;\u4e4b\u540e\u7531\u8be5\u51fd\u6570\u7684\u6240\u6709\u8c03\u7528\u5171\u4eab<\/p>\n<\/li>\n<\/ul>\n<p>def f(x, lst&#061;[]):    # \u2190 \u6267\u884c def \u8fd9\u4e00\u884c\u7684\u77ac\u95f4&#xff0c;[] \u88ab\u521b\u5efa&#xff0c;\u5e76\u7ed1\u5b9a\u5230 f \u4e0a<br \/>\n    pass<\/p>\n<ul>\n<li>\n<p>\u4f4d\u7f6e\u9ed8\u8ba4\u53c2\u6570\u4fdd\u5b58\u5728\u51fd\u6570\u5bf9\u8c61\u7684 __defaults__ \u5c5e\u6027\u4e2d&#xff08;\u5143\u7ec4&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u4fdd\u5b58\u5728 __kwdefaults__ \u5c5e\u6027\u4e2d&#xff08;\u5b57\u5178&#xff09;<\/p>\n<\/li>\n<\/ul>\n<p>\u4e24\u8005\u7684\u5bf9\u6bd4\u5982\u4e0b&#xff1a;<\/p>\n<table>\n<tr>\u5bf9\u6bd4\u9879__defaults____kwdefaults__<\/tr>\n<tbody>\n<tr>\n<td>\u4fdd\u5b58\u5185\u5bb9<\/td>\n<td>\u4f4d\u7f6e\u53c2\u6570 \/ \u9ed8\u8ba4\u53c2\u6570\u7684\u9ed8\u8ba4\u503c<\/td>\n<td>\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570\u7684\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u636e\u7c7b\u578b<\/td>\n<td>\u5143\u7ec4&#xff08;tuple&#xff09;<\/td>\n<td>\u5b57\u5178&#xff08;dict&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u952e \/ \u987a\u5e8f<\/td>\n<td>\u6309\u53c2\u6570\u5b9a\u4e49\u987a\u5e8f\u6392\u5217&#xff0c;\u65e0\u952e\u540d<\/td>\n<td>\u4ee5\u53c2\u6570\u540d\u4e3a\u952e&#xff0c;\u65e0\u5e8f<\/td>\n<\/tr>\n<tr>\n<td>\u8bbf\u95ee\u65b9\u5f0f<\/td>\n<td>func.__defaults__<\/td>\n<td>func.__kwdefaults__<\/td>\n<\/tr>\n<tr>\n<td>\u65e0\u9ed8\u8ba4\u503c\u65f6<\/td>\n<td>None<\/td>\n<td>None<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u539f\u5730\u4fee\u6539&#xff08;append \/ extend \/ &#043;&#061; \u7b49&#xff09;<\/p>\n<p>\u6240\u6709\u8c03\u7528\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u4f1a\u770b\u5230\u5f7c\u6b64\u7684\u4fee\u6539\u3002<\/p>\n<p>def add(item, lst&#061;[]):<br \/>\n    lst.append(item)      # \u539f\u5730\u4fee\u6539&#xff1a;\u6539\u7684\u662f\u9ed8\u8ba4\u90a3\u4e2a\u5217\u8868\u672c\u8eab<br \/>\n    return lst<br \/>\nprint(add(&#034;a&#034;))   # [&#039;a&#039;]<br \/>\nprint(add(&#034;b&#034;))   # [&#039;a&#039;, &#039;b&#039;]   \u2190 \u770b\u5230\u4e86\u4e0a\u4e00\u6b21\u7684\u4fee\u6539<br \/>\nprint(add(&#034;c&#034;))   # [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<\/p>\n<p>\u91cd\u65b0\u8d4b\u503c&#xff1a;\u521b\u5efa\u65b0\u5bf9\u8c61&#xff0c;\u4e0d\u5f71\u54cd\u9ed8\u8ba4\u503c<\/p>\n<p>def reset(lst&#061;[]):<br \/>\n    lst &#061; [1, 2, 3]       # \u91cd\u65b0\u8d4b\u503c&#xff1a;lst \u6539\u6307\u5411\u65b0\u5217\u8868&#xff0c;\u539f\u9ed8\u8ba4\u5217\u8868\u6ca1\u52a8<br \/>\n    return lst<br \/>\nprint(reset())    # [1, 2, 3]<br \/>\nprint(reset())    # [1, 2, 3]<br \/>\nprint(reset())    # [1, 2, 3]   \u2190 \u6bcf\u6b21\u90fd\u662f\u65b0\u7684<\/p>\n<p>\u9ed8\u8ba4\u53c2\u6570\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b&#xff1a;\u4e0d\u53ef\u53d8\u5bf9\u8c61\u7684\u4fee\u6539\u672c\u8d28\u662f\u7ed1\u5b9a\u65b0\u7684\u6570\u636e\u5bf9\u8c61&#xff0c;\u9ed8\u8ba4\u53c2\u6570\u4e0d\u53d8<\/p>\n<p>def f(x&#061;10, s&#061;&#034;hi&#034;):<br \/>\n    x &#043;&#061; 1        # \u7b49\u4ef7\u4e8e x &#061; x &#043; 1&#xff0c;\u521b\u5efa\u65b0 int&#xff0c;\u91cd\u65b0\u7ed1\u5b9a<br \/>\n    s &#043;&#061; &#034;!&#034;      # \u7b49\u4ef7\u4e8e s &#061; s &#043; &#034;!&#034;&#xff0c;\u521b\u5efa\u65b0 str&#xff0c;\u91cd\u65b0\u7ed1\u5b9a<br \/>\n    return x, s<br \/>\nprint(f())   # (11, &#039;hi!&#039;)<br \/>\nprint(f())   # (11, &#039;hi!&#039;)   \u2190 \u6bcf\u6b21\u4e00\u6837&#xff0c;\u9ed8\u8ba4\u503c\u6ca1\u53d8<br \/>\nprint(f())   # (11, &#039;hi!&#039;)<\/p>\n<h4>4.2.4 \u53ef\u53d8\u53c2\u6570<\/h4>\n<h5>*args \u2014\u2014 \u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570<\/h5>\n<ul>\n<li>\n<p>*args \u5c06\u53c2\u6570\u5c01\u88c5\u4e3a tuple<\/p>\n<ul>\n<li>\n<p>\u4e0d\u4f20\u65f6 args &#061; ()<\/p>\n<\/li>\n<li>\n<p>\u4f20\u591a\u4e2a\u65f6\u6309\u987a\u5e8f\u5c01\u88c5&#xff0c;\u5982 f(1,2,3) \u2192 args &#061; (1, 2, 3)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5e38\u4e0e\u786e\u5b9a\u53c2\u6570\u914d\u5408&#xff1a;def f(name, *args)<\/p>\n<ul>\n<li>\n<p>\u786e\u5b9a\u53c2\u6570\u5148\u62ff\u503c&#xff0c;\u5269\u4f59\u4f4d\u7f6e\u5b9e\u53c2\u8fdb args<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def show(*args):<br \/>\n    print(&#034;args &#061;&#034;, args)<br \/>\n    print(&#034;\u7c7b\u578b &#061;&#034;, type(args))<br \/>\n# \u2460 \u4e0d\u4f20\u65f6 args &#061; ()<br \/>\nshow()<br \/>\nargs &#061; ()<br \/>\n# \u7c7b\u578b &#061; &lt;class &#039;tuple&#039;&gt;<br \/>\n# \u2461 \u4f20\u591a\u4e2a\u65f6\u6309\u987a\u5e8f\u5c01\u88c5\u4e3a tuple<br \/>\nshow(1, 2, 3)<br \/>\nargs &#061; (1, 2, 3)<br \/>\n# \u7c7b\u578b &#061; &lt;class &#039;tuple&#039;&gt;<br \/>\n# \u2462 \u4e0e\u786e\u5b9a\u53c2\u6570\u914d\u5408&#xff1a;\u786e\u5b9a\u53c2\u6570\u5148\u62ff\u503c&#xff0c;\u5269\u4f59\u4f4d\u7f6e\u5b9e\u53c2\u8fdb args<br \/>\ndef introduce(name, *hobbies):<br \/>\n    print(f&#034;\u59d3\u540d&#xff1a;{name}&#xff0c;\u7231\u597d&#xff1a;{hobbies}&#034;)<br \/>\nintroduce(&#034;\u5c0f\u660e&#034;)<br \/>\n# \u59d3\u540d&#xff1a;\u5c0f\u660e&#xff0c;\u7231\u597d&#xff1a;()<br \/>\nintroduce(&#034;\u5c0f\u660e&#034;, &#034;\u7bee\u7403&#034;, &#034;\u7f16\u7a0b&#034;)<br \/>\n# \u59d3\u540d&#xff1a;\u5c0f\u660e&#xff0c;\u7231\u597d&#xff1a;(&#039;\u7bee\u7403&#039;, &#039;\u7f16\u7a0b&#039;)<\/p>\n<h5>**kwargs \u2014\u2014 \u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570<\/h5>\n<ul>\n<li>\n<p>**kwargs \u5c06\u53c2\u6570\u5c01\u88c5\u4e3a dict<\/p>\n<ul>\n<li>\n<p>\u4e0d\u4f20\u65f6 kwargs &#061; {}<\/p>\n<\/li>\n<li>\n<p>\u4f20\u591a\u4e2a\u65f6\u6309 \u952e&#061;\u503c \u5c01\u88c5&#xff0c;\u5982 f(a&#061;1,b&#061;2) \u2192 kwargs &#061; {&#039;a&#039;: 1, &#039;b&#039;: 2}<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5e38\u4e0e\u786e\u5b9a\u53c2\u6570\u914d\u5408&#xff1a;def f(name, **kwargs)<\/p>\n<ul>\n<li>\n<p>\u786e\u5b9a\u53c2\u6570\u5148\u62ff\u503c&#xff0c;\u5269\u4f59\u5173\u952e\u5b57\u5b9e\u53c2\u8fdb kwargs<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def show(**kwargs):<br \/>\n    print(&#034;kwargs &#061;&#034;, kwargs)<br \/>\n    print(&#034;\u7c7b\u578b &#061;&#034;, type(kwargs))<br \/>\n# \u2460 \u4e0d\u4f20\u65f6 kwargs &#061; {}<br \/>\nshow()<br \/>\nkwargs &#061; {}<br \/>\n# \u7c7b\u578b &#061; &lt;class &#039;dict&#039;&gt;<br \/>\n# \u2461 \u4f20\u591a\u4e2a\u65f6\u6309 \u952e&#061;\u503c \u5c01\u88c5\u4e3a dict<br \/>\nshow(a&#061;1, b&#061;2, c&#061;3)<br \/>\nkwargs &#061; {&#039;a&#039;: 1, &#039;b&#039;: 2, &#039;c&#039;: 3}<br \/>\n# \u2462 \u4e0e\u786e\u5b9a\u53c2\u6570\u914d\u5408&#xff1a;\u786e\u5b9a\u53c2\u6570\u5148\u62ff\u503c&#xff0c;\u5269\u4f59\u5173\u952e\u5b57\u5b9e\u53c2\u8fdb kwargs<br \/>\ndef register(name, **info):<br \/>\n    print(f&#034;\u59d3\u540d&#xff1a;{name}&#xff0c;\u5176\u4ed6\u4fe1\u606f&#xff1a;{info}&#034;)<br \/>\nregister(&#034;\u5c0f\u660e&#034;)<br \/>\n# \u59d3\u540d&#xff1a;\u5c0f\u660e&#xff0c;\u5176\u4ed6\u4fe1\u606f&#xff1a;{}<br \/>\nregister(&#034;\u5c0f\u660e&#034;, age&#061;18, city&#061;&#034;\u5317\u4eac&#034;)<br \/>\n# \u59d3\u540d&#xff1a;\u5c0f\u660e&#xff0c;\u5176\u4ed6\u4fe1\u606f&#xff1a;{&#039;age&#039;: 18, &#039;city&#039;: &#039;\u5317\u4eac&#039;}<\/p>\n<h5>\u8c03\u7528\u65f6\u7684\u89e3\u5305<\/h5>\n<ul>\n<li>\n<p>\u7528 * \u89e3\u5305\u5e8f\u5217\u4e3a\u4f4d\u7f6e\u5b9e\u53c2<\/p>\n<ul>\n<li>\n<p>demo(*[1, 2, 3]) \u7b49\u4ef7\u4e8e demo(1, 2, 3)<\/p>\n<\/li>\n<li>\n<p>demo(*(1, 2, 3)) \u540c\u6837\u9002\u7528<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u7528 ** \u89e3\u5305\u5b57\u5178\u4e3a\u5173\u952e\u5b57\u5b9e\u53c2<\/p>\n<ul>\n<li>\n<p>demo(**{&#034;a&#034;: 1}) \u7b49\u4ef7\u4e8e demo(a&#061;1)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>* \u548c ** \u53ef\u540c\u65f6\u4f7f\u7528<\/p>\n<ul>\n<li>\n<p>demo(*[1, 2], **{&#034;c&#034;: 3}) \u2192 demo(1, 2, c&#061;3)<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p># \u2460 \u7528 * \u89e3\u5305\u5e8f\u5217\u4e3a\u4f4d\u7f6e\u5b9e\u53c2<br \/>\ndef my_sum(a, b, c):<br \/>\n    return a &#043; b &#043; c<br \/>\n# *\u5bb9\u5668<br \/>\nprint(my_sum(*[1, 2, 3]))    # \u7b49\u4ef7\u4e8e my_sum(1, 2, 3) \u2192 6<br \/>\nprint(my_sum(*(1, 2, 3)))    # 6<br \/>\n# \u2461 \u7528 ** \u89e3\u5305\u5b57\u5178\u4e3a\u5173\u952e\u5b57\u5b9e\u53c2<br \/>\ndef introduce(name, age):<br \/>\n    print(f&#034;{name}&#xff0c;{age}\u5c81&#034;)<br \/>\ninfo &#061; {&#034;name&#034;: &#034;\u5c0f\u660e&#034;, &#034;age&#034;: 18}<br \/>\nintroduce(**info)            # \u7b49\u4ef7\u4e8e introduce(name&#061;&#034;\u5c0f\u660e&#034;, age&#061;18)<br \/>\n# \u5c0f\u660e&#xff0c;18\u5c81<br \/>\n# \u2462 * \u548c ** \u53ef\u540c\u65f6\u4f7f\u7528<br \/>\ndef demo(a, b, c, d):<br \/>\n    print(a, b, c, d)<br \/>\nargs &#061; [1, 2]<br \/>\nkwargs &#061; {&#034;c&#034;: 3, &#034;d&#034;: 4}<br \/>\ndemo(*args, **kwargs)        # 1 2 3 4<\/p>\n<h5>\u6613\u9519\u70b9\u603b\u7ed3<\/h5>\n<ul>\n<li>\n<p>\u5b9a\u4e49\u65f6 *args \/ **kwargs \u662f\u6536\u96c6\u53c2\u6570&#xff1b;\u8c03\u7528\u65f6 * \/ ** \u662f\u89e3\u5305\u53c2\u6570<\/p>\n<\/li>\n<li>\n<p>*args \u5fc5\u987b\u5199\u5728 **kwargs \u524d\u9762<\/p>\n<\/li>\n<li>\n<p>\u4f4d\u7f6e\u5b9e\u53c2\u4e0d\u80fd\u5199\u5728\u5173\u952e\u5b57\u5b9e\u53c2\u540e\u9762&#xff0c;\u5982 f(a&#061;1, 2) \u4f1a\u62a5\u9519<\/p>\n<\/li>\n<li>\n<p>*args \u540e\u9762\u7684\u53c2\u6570\u81ea\u52a8\u6210\u4e3a\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff0c;\u5982 def f(a, *args, b) \u4e2d b \u5fc5\u987b\u7528\u5173\u952e\u5b57\u4f20\u53c2<\/p>\n<\/li>\n<\/ul>\n<p># \u2460 \u5b9a\u4e49\u65f6&#034;\u6536\u96c6&#034; vs \u8c03\u7528\u65f6&#034;\u89e3\u5305&#034;<br \/>\ndef f(*args, **kwargs):<br \/>\n    print(args, kwargs)<br \/>\ndata &#061; [1, 2, 3]<br \/>\ninfo &#061; {&#034;x&#034;: 10}<br \/>\nf(*data, **info)             # (1, 2, 3) {&#039;x&#039;: 10}<br \/>\n\u2461 *args \u5fc5\u987b\u5199\u5728 **kwargs \u524d\u9762<br \/>\ndef ok(*args, **kwargs):<br \/>\npass<br \/>\ndef bad(**kwargs, *args):  # SyntaxError<br \/>\npass<br \/>\n\u2462 \u4f4d\u7f6e\u5b9e\u53c2\u4e0d\u80fd\u5199\u5728\u5173\u952e\u5b57\u5b9e\u53c2\u540e\u9762<br \/>\ndef g(a, b):<br \/>\npass<br \/>\ng(1, b&#061;2)        # \u6b63\u786e<br \/>\ng(a&#061;1, 2)      # SyntaxError: positional argument follows keyword argument<br \/>\n\u2463 *args \u540e\u9762\u7684\u53c2\u6570\u81ea\u52a8\u6210\u4e3a\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570<br \/>\ndef h(a, *args, b):<br \/>\nprint(a, args, b)<br \/>\nh(1, 2, 3, b&#061;4)      # 1 (2, 3) 4<br \/>\nh(1, 2, 3, 4)      # TypeError: missing 1 required keyword-only argument: &#039;b&#039;<br \/>\n\u2464 \u7a7a\u503c\u5224\u65ad<br \/>\ndef check(*args, **kwargs):<br \/>\nif not args:<br \/>\nprint(&#034;\u6ca1\u6709\u4f4d\u7f6e\u53c2\u6570&#034;)<br \/>\nif not kwargs:<br \/>\nprint(&#034;\u6ca1\u6709\u5173\u952e\u5b57\u53c2\u6570&#034;)<br \/>\ncheck()              # \u6ca1\u6709\u4f4d\u7f6e\u53c2\u6570 \/ \u6ca1\u6709\u5173\u952e\u5b57\u53c2\u6570<br \/>\ncheck(1)             # \u6ca1\u6709\u5173\u952e\u5b57\u53c2\u6570<br \/>\ncheck(a&#061;1)           # \u6ca1\u6709\u4f4d\u7f6e\u53c2\u6570<\/p>\n<h4>4.2.5 \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570 *<\/h4>\n<h5>\u57fa\u672c\u8bed\u6cd5<\/h5>\n<ul>\n<li>\n<p>def demo(a, *, b): \u540e\u9762\u7684\u53c2\u6570\u8c03\u7528\u5fc5\u987b\u4f7f\u7528\u5173\u952e\u5b57\u4f20\u53c2<\/p>\n<ul>\n<li>\n<p>b \u53ea\u80fd\u901a\u8fc7 b&#061;\u503c \u4f20\u9012&#xff0c;\u4e0d\u80fd\u7528\u4f4d\u7f6e\u4f20\u53c2<\/p>\n<\/li>\n<li>\n<p>\u4f4d\u7f6e\u4f20\u53c2\u4f1a\u62a5 TypeError<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def demo(a, *, b):<br \/>\n    print(&#034;a &#061;&#034;, a)<br \/>\n    print(&#034;b &#061;&#034;, b)<br \/>\n# b \u7528\u5173\u952e\u5b57\u4f20\u53c2<br \/>\ndemo(1, b&#061;2)<br \/>\na &#061; 1<br \/>\nb &#061; 2<br \/>\n# b \u7528\u4f4d\u7f6e\u4f20\u53c2\u62a5\u9519<br \/>\n# demo(1, 2)<br \/>\n# TypeError: demo() takes 1 positional argument but 2 were given<\/p>\n<h5>* \u7684\u672c\u8d28<\/h5>\n<ul>\n<li>\n<p>* \u672c\u8eab\u4e0d\u662f\u53c2\u6570&#xff0c;\u53ea\u662f\u4e00\u4e2a\u5206\u9694\u7b26<\/p>\n<ul>\n<li>\n<p>\u5b83\u4e0d\u63a5\u6536\u4efb\u4f55\u503c&#xff0c;\u4e5f\u4e0d\u5360\u7528\u53c2\u6570\u4f4d\u7f6e<\/p>\n<\/li>\n<li>\n<p>\u4f5c\u7528\u662f\u628a\u540e\u9762\u7684\u53c2\u6570&#034;\u6807\u8bb0&#034;\u4e3a\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def demo(a, *, b):<br \/>\n    print(&#034;a &#061;&#034;, a, &#034;b &#061;&#034;, b)<br \/>\n# * \u4e0d\u63a5\u6536\u4efb\u4f55\u503c&#xff0c;\u53ea\u8d77\u5206\u9694\u4f5c\u7528<br \/>\ndemo(1, b&#061;2)     # a &#061; 1 b &#061; 2<br \/>\n# \u4e0d\u80fd\u7ed9 * \u4f20\u503c<br \/>\n# demo(1, 2, b&#061;3)<br \/>\n# TypeError: demo() takes 1 positional argument but 3 were given<\/p>\n<h5>*args \u540e\u9762\u7684\u53c2\u6570\u81ea\u52a8\u6210\u4e3a\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570<\/h5>\n<ul>\n<li>\n<p>\u4f8b\u5982 def f(a, *args, b) \u4e2d b \u5fc5\u987b\u7528\u5173\u952e\u5b57\u4f20\u53c2<\/p>\n<ul>\n<li>\n<p>\u56e0\u4e3a\u6240\u6709\u591a\u4f59\u7684\u4f4d\u7f6e\u5b9e\u53c2\u90fd\u88ab *args \u622a\u8d70\u4e86<\/p>\n<\/li>\n<li>\n<p>b \u65e0\u6cd5\u518d\u901a\u8fc7\u4f4d\u7f6e\u4f20\u53c2&#xff0c;\u53ea\u80fd\u7528\u5173\u952e\u5b57<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def f(a, *args, b):<br \/>\n    print(&#034;a &#061;&#034;, a)<br \/>\n    print(&#034;args &#061;&#034;, args)<br \/>\n    print(&#034;b &#061;&#034;, b)<br \/>\n# b \u7528\u5173\u952e\u5b57\u4f20\u53c2<br \/>\nf(1, 2, 3, b&#061;4)<br \/>\na &#061; 1<br \/>\nargs &#061; (2, 3)<br \/>\nb &#061; 4<br \/>\n# \u4e0d\u4f20 b \u62a5\u9519<br \/>\n# f(1, 2, 3)<br \/>\n# TypeError: f() missing 1 required keyword-only argument: &#039;b&#039;<br \/>\n# \u628a b \u5f53\u4f4d\u7f6e\u4f20\u53c2\u4e5f\u62a5\u9519<br \/>\n# f(1, 2, 3, 4)<br \/>\n# TypeError: f() missing 1 required keyword-only argument: &#039;b&#039;<\/p>\n<h5>\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570\u53ef\u4ee5\u540c\u65f6\u6709\u9ed8\u8ba4\u503c<\/h5>\n<ul>\n<li>\n<p>\u4f8b\u5982 def f(a, *, b&#061;1):<\/p>\n<ul>\n<li>\n<p>\u4e0d\u4f20 b \u65f6\u4f7f\u7528\u9ed8\u8ba4\u503c 1<\/p>\n<\/li>\n<li>\n<p>\u4f20 b \u65f6\u5fc5\u987b\u7528\u5173\u952e\u5b57&#xff0c;\u5982 f(1, b&#061;5)<\/p>\n<\/li>\n<li>\n<p>\u5373\u4f7f\u6709\u9ed8\u8ba4\u503c&#xff0c;\u4ecd\u7136\u4e0d\u80fd\u7528\u4f4d\u7f6e\u4f20\u53c2&#xff0c;\u5982 f(1, 5) \u62a5\u9519<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>def f(a, *, b&#061;1):<br \/>\n    print(&#034;a &#061;&#034;, a, &#034;b &#061;&#034;, b)<br \/>\n# \u4e0d\u4f20 b&#xff0c;\u7528\u9ed8\u8ba4\u503c<br \/>\nf(1)<br \/>\na &#061; 1<br \/>\nb &#061; 1<br \/>\n# \u4f20 b&#xff0c;\u5fc5\u987b\u7528\u5173\u952e\u5b57<br \/>\nf(1, b&#061;5)<br \/>\na &#061; 1<br \/>\nb &#061; 5<br \/>\n# \u5373\u4f7f\u6709\u9ed8\u8ba4\u503c&#xff0c;\u4e5f\u4e0d\u80fd\u7528\u4f4d\u7f6e\u4f20<br \/>\n# f(1, 5)<br \/>\n# TypeError: f() takes 1 positional argument but 2 were given<\/p>\n<h5>\u6613\u9519\u70b9\u603b\u7ed3<\/h5>\n<ul>\n<li>\n<p>* \u4e0d\u662f\u53c2\u6570&#xff0c;\u4e0d\u80fd\u7ed9\u5b83\u4f20\u503c<\/p>\n<\/li>\n<li>\n<p>* \u540e\u9762\u7684\u53c2\u6570\u4e0d\u80fd\u9760\u4f4d\u7f6e\u4f20<\/p>\n<\/li>\n<li>\n<p>\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570\u5373\u4f7f\u6709\u9ed8\u8ba4\u503c&#xff0c;\u4e5f\u4e0d\u80fd\u7528\u4f4d\u7f6e\u4f20<\/p>\n<\/li>\n<li>\n<p>\u53c2\u6570\u987a\u5e8f\u4e0d\u80fd\u98a0\u5012&#xff0c;*args \u5fc5\u987b\u5728 **kwargs \u524d<\/p>\n<\/li>\n<\/ul>\n<p># &#061;&#061;&#061;&#061;&#061; \u6613\u9519\u70b9\u96c6\u4e2d\u6f14\u793a &#061;&#061;&#061;&#061;&#061;<br \/>\n# \u2460 * \u4e0d\u662f\u53c2\u6570&#xff0c;\u4e0d\u80fd\u7ed9\u5b83\u4f20\u503c<br \/>\ndef f1(a, *, b):<br \/>\n    print(&#034;f1:&#034;, a, b)<br \/>\nf1(1, b&#061;2)        # a&#061;1 b&#061;2<br \/>\n# f1(1, 2, b&#061;3)   # TypeError: \u591a\u51fa\u7684 2 \u65e0\u5904\u53ef\u53bb<br \/>\n# \u2461 * \u540e\u9762\u7684\u53c2\u6570\u4e0d\u80fd\u9760\u4f4d\u7f6e\u4f20<br \/>\ndef f2(a, *, b):<br \/>\n    print(&#034;f2:&#034;, a, b)<br \/>\nf2(1, b&#061;2)        # a&#061;1 b&#061;2<br \/>\n# f2(1, 2)        # TypeError: b \u5fc5\u987b\u7528\u5173\u952e\u5b57\u4f20\u53c2<br \/>\n# \u2462 \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570\u5373\u4f7f\u6709\u9ed8\u8ba4\u503c&#xff0c;\u4e5f\u4e0d\u80fd\u7528\u4f4d\u7f6e\u4f20<br \/>\ndef f3(a, *, b&#061;1):<br \/>\n    print(&#034;f3:&#034;, a, b)<br \/>\nf3(1)             # a&#061;1 b&#061;1&#xff08;\u7528\u9ed8\u8ba4\u503c&#xff09;<br \/>\nf3(1, b&#061;2)        # a&#061;1 b&#061;2&#xff08;\u7528\u5173\u952e\u5b57&#xff09;<br \/>\n# f3(1, 2)        # TypeError: \u6709\u9ed8\u8ba4\u503c\u4e5f\u4e0d\u80fd\u4f4d\u7f6e\u4f20<br \/>\n# \u2463 \u53c2\u6570\u987a\u5e8f\u4e0d\u80fd\u98a0\u5012&#xff1a;*args \u5fc5\u987b\u5728 **kwargs \u524d<br \/>\ndef f4(a, *, b, **kwargs):<br \/>\n    print(&#034;f4:&#034;, a, b, kwargs)<br \/>\nf4(1, b&#061;2, x&#061;3)   # a&#061;1 b&#061;2 {&#039;x&#039;: 3}<br \/>\n# def bad(a, **kwargs, *, b):   # SyntaxError: **kwargs \u540e\u4e0d\u80fd\u518d\u6709\u53c2\u6570<br \/>\n#     pass<\/p>\n<h4>4.2.6 \u53c2\u6570\u7684\u5b9a\u4e49\u987a\u5e8f<\/h4>\n<p>\u4f4d\u7f6e\u53c2\u6570 &gt; \u9ed8\u8ba4\u53c2\u6570 &gt; *args \u53ef\u53d8\u4f4d\u7f6e &gt; \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570 &gt; **kwargs \u53ef\u53d8\u5173\u952e\u5b57<\/p>\n<p>def f(a, b&#061;1, *args, c, d&#061;2, **kwargs):<br \/>\n    pass<br \/>\n#   \u2191   \u2191     \u2191      \u2191   \u2191       \u2191<br \/>\n#  \u4f4d\u7f6e \u9ed8\u8ba4  \u53ef\u53d8\u4f4d\u7f6e \u4ec5\u9650\u5173\u952e\u5b57  \u53ef\u53d8\u5173\u952e\u5b57<\/p>\n<ul>\n<li>\n<p>\u4f4d\u7f6e\u53c2\u6570&#xff1a;\u5fc5\u4f20&#xff0c;\u6309\u4f4d\u7f6e\u4e00\u4e00\u5bf9\u5e94<\/p>\n<\/li>\n<li>\n<p>\u9ed8\u8ba4\u53c2\u6570&#xff1a;\u53ef\u7701&#xff0c;\u5fc5\u987b\u653e\u5728\u4f4d\u7f6e\u53c2\u6570\u4e4b\u540e<\/p>\n<\/li>\n<li>\n<p>*args&#xff1a;\u6536\u96c6\u591a\u4f59\u4f4d\u7f6e\u5b9e\u53c2&#xff0c;\u5c01\u88c5\u4e3a tuple<\/p>\n<\/li>\n<li>\n<p>\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff1a;* \u6216 *args \u4e4b\u540e\u7684\u53c2\u6570&#xff0c;\u53ea\u80fd\u5173\u952e\u5b57\u4f20\u53c2<\/p>\n<\/li>\n<li>\n<p>**kwargs&#xff1a;\u6536\u96c6\u591a\u4f59\u5173\u952e\u5b57\u5b9e\u53c2&#xff0c;\u5c01\u88c5\u4e3a dict&#xff0c;\u5fc5\u987b\u653e\u6700\u540e<\/p>\n<\/li>\n<\/ul>\n<h3>4.3 \u8fd4\u56de\u503c<\/h3>\n<ul>\n<li>\u8bed\u6cd5&#xff1a;return\n<ul>\n<li>return \u53ef\u4ee5\u4e0d\u5e26\u503c&#xff0c;\u7b49\u4ef7\u4e8e return None<\/li>\n<li>\n<p>return \u7ec8\u6b62\u51fd\u6570&#xff0c;\u540e\u9762\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p># \u8bed\u6cd5&#xff1a;return<br \/>\ndef add(a, b):<br \/>\n    return a &#043; b<br \/>\nresult &#061; add(1, 2)<br \/>\nprint(result)   # 3<br \/>\n# return \u53ef\u4ee5\u4e0d\u5e26\u503c&#xff0c;\u7b49\u4ef7\u4e8e return None<br \/>\ndef f():<br \/>\n    return<br \/>\nprint(f())          # None<br \/>\nprint(f() is None)  # True<br \/>\n# return \u7ec8\u6b62\u51fd\u6570&#xff0c;\u540e\u9762\u7684\u4ee3\u7801\u4e0d\u4f1a\u6267\u884c<br \/>\ndef f():<br \/>\n    print(&#034;\u5f00\u59cb&#034;)<br \/>\n    return<br \/>\nprint(&#034;\u8fd9\u884c\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c&#034;)<br \/>\nf()<\/p>\n<ul>\n<li>\u65e0 return&#xff1a;\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56deNone<\/li>\n<\/ul>\n<p>def f():<br \/>\n    print(&#034;hello&#034;)<br \/>\nresult &#061; f()<br \/>\n# hello<br \/>\nprint(result)   # None<\/p>\n<ul>\n<li>\u591a\u8fd4\u56de\u503c&#xff1a;return a,b,c&#xff0c;\u672c\u8d28\u662f\u8fd4\u56de\u5143\u7ec4tuple&#xff0c;\u53ef\u4ee5\u7528\u591a\u4e2a\u53d8\u91cf\u63a5\u6536<\/li>\n<li>\u591a\u4e2a\u53d8\u91cf\u63a5\u6536\u65f6&#xff0c;\u53d8\u91cf\u6570\u91cf\u5fc5\u987b\u4e0e\u8fd4\u56de\u503c\u6570\u91cf\u4e00\u81f4&#xff0c;\u5426\u5219\u62a5ValueError<\/li>\n<li>\u53ef\u4ee5\u7528_\u5ffd\u7565\u67d0\u4e2a\u8fd4\u56de\u503c&#xff0c;\u4f8b\u5982 a, _, c &#061; func()<\/li>\n<\/ul>\n<p># \u591a\u8fd4\u56de\u503c&#xff1a;return a,b,c&#xff0c;\u672c\u8d28\u662f\u8fd4\u56de\u5143\u7ec4 tuple&#xff0c;\u53ef\u4ee5\u7528\u591a\u4e2a\u53d8\u91cf\u63a5\u6536<br \/>\ndef f():<br \/>\n    return 1, 2, 3<br \/>\nresult &#061; f()<br \/>\nprint(result)         # (1, 2, 3)<br \/>\nprint(type(result))   # &lt;class &#039;tuple&#039;&gt;<br \/>\na, b, c &#061; f()<br \/>\nprint(a, b, c)        # 1 2 3<br \/>\n# \u591a\u4e2a\u53d8\u91cf\u63a5\u6536\u65f6&#xff0c;\u53d8\u91cf\u6570\u91cf\u5fc5\u987b\u4e0e\u8fd4\u56de\u503c\u6570\u91cf\u4e00\u81f4&#xff0c;\u5426\u5219\u62a5 ValueError<br \/>\ndef f():<br \/>\n    return 1, 2, 3<br \/>\n# a, b &#061; f() # ValueError: not enough values to unpack (expected 2, got 3)<br \/>\n# a, b, c, d &#061; f() # ValueError: too many values to unpack (expected 4)<br \/>\n# \u53ef\u4ee5\u7528 _ \u5ffd\u7565\u67d0\u4e2a\u8fd4\u56de\u503c&#xff0c;\u4f8b\u5982 a, _, c &#061; func()<br \/>\ndef f():<br \/>\n    return 1, 2, 3<br \/>\na, _, c &#061; f()<br \/>\nprint(a, c)   # 1 3<\/p>\n<ul>\n<li>return \u53ea\u80fd\u7ec8\u6b62\u5f53\u524d\u51fd\u6570&#xff0c;\u4e0d\u4f1a\u7ec8\u6b62\u5916\u5c42\u51fd\u6570&#xff08;\u533a\u5206\u5168\u5c40 \/ \u5d4c\u5957&#xff09;<\/li>\n<\/ul>\n<p># return \u53ea\u80fd\u7ec8\u6b62\u5f53\u524d\u51fd\u6570&#xff0c;\u4e0d\u4f1a\u7ec8\u6b62\u5916\u5c42\u51fd\u6570&#xff08;\u533a\u5206\u5168\u5c40 \/ \u5d4c\u5957&#xff09;<\/p>\n<p>def outer():<br \/>\n    print(&#034;outer \u5f00\u59cb&#034;)<br \/>\n    def inner():<br \/>\n        print(&#034;inner \u5f00\u59cb&#034;)<br \/>\n        return<br \/>\n        print(&#034;inner \u8fd9\u884c\u4e0d\u6267\u884c&#034;)<br \/>\n    inner()<br \/>\nprint(&#034;outer \u7ee7\u7eed\u6267\u884c&#034;)   # return \u53ea\u7ec8\u6b62 inner&#xff0c;\u4e0d\u5f71\u54cd outer<br \/>\nouter()<\/p>\n<h3>4.4 \u51fd\u6570\u4f5c\u7528\u57df<\/h3>\n<h4>4.4.1 \u53d8\u91cf\u4f5c\u7528\u57df\u5206\u7c7b<\/h4>\n<ul>\n<li>\u5c40\u90e8\u53d8\u91cf&#xff1a;\u51fd\u6570\u5185\u90e8\u5b9a\u4e49&#xff0c;\u4ec5\u51fd\u6570\u5185\u53ef\u7528<\/li>\n<li>\u5168\u5c40\u53d8\u91cf&#xff1a;\u51fd\u6570\u5916\u90e8\u5b9a\u4e49&#xff0c;\u6574\u4e2a\u6587\u4ef6\u53ef\u7528<\/li>\n<\/ul>\n<p># \u5168\u5c40\u53d8\u91cf<br \/>\n# \u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49&#xff0c;\u6574\u4e2a\u6587\u4ef6\u53ef\u7528<br \/>\nname &#061; &#034;\u5c0f\u660e&#034;          # \u5168\u5c40\u53d8\u91cf<br \/>\nage &#061; 18<br \/>\ndef show_global():<br \/>\n# \u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u8bfb\u53d6\u5168\u5c40\u53d8\u91cf<br \/>\n    print(&#034;\u5e74\u9f84&#xff1a;&#034;, age)<br \/>\nshow_global()<br \/>\n# \u5e74\u9f84&#xff1a; 18<br \/>\n# \u5c40\u90e8\u53d8\u91cf<br \/>\n# \u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49&#xff0c;\u4ec5\u51fd\u6570\u5185\u53ef\u7528<br \/>\ndef show_local():<br \/>\n    name&#061; &#034;zs&#034;      # \u5c40\u90e8\u53d8\u91cf<br \/>\nprint(&#034;\u59d3\u540d&#xff1a;&#034;, name)<br \/>\nshow_local()<br \/>\n# \u59d3\u540d&#xff1a; zs<br \/>\n#\u51fd\u6570\u5916\u90e8\u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf&#xff0c;\u62a5 NameError<br \/>\n# print(city) # NameError: name &#039;city&#039; is not defined<\/p>\n<h4>4.4.2 \u5173\u952e\u5b57<\/h4>\n<h5>global&#xff1a;\u51fd\u6570\u5185\u58f0\u660e&#xff0c;\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/h5>\n<ul>\n<li>\u5168\u5c40\u5df2\u5b58\u5728&#xff1a;\u76f4\u63a5\u4fee\u6539\u8be5\u5168\u5c40\u53d8\u91cf<\/li>\n<\/ul>\n<p>count &#061; 0<br \/>\ndef add():<br \/>\nglobal count      # \u58f0\u660e count \u662f\u5168\u5c40\u53d8\u91cf<br \/>\ncount &#043;&#061; 1<br \/>\nadd()<br \/>\nadd()<br \/>\nprint(count)          # 2<\/p>\n<ul>\n<li>\u5168\u5c40\u4e0d\u5b58\u5728&#xff1a;\u8d4b\u503c\u65f6\u81ea\u52a8\u5728\u5168\u5c40\u521b\u5efa<\/li>\n<\/ul>\n<p>def f():<br \/>\n    global x          # \u5168\u5c40\u539f\u672c\u6ca1\u6709 x<br \/>\n    x &#061; 10            # \u8d4b\u503c\u65f6\u81ea\u52a8\u5728\u5168\u5c40\u521b\u5efa<br \/>\nf()<br \/>\nprint(x)              # 10<\/p>\n<ul>\n<li>\u4e0d\u52a0 global \u65f6&#xff0c;\u51fd\u6570\u5185\u8d4b\u503c\u4f1a\u521b\u5efa\u5c40\u90e8\u53d8\u91cf<\/li>\n<\/ul>\n<p>count &#061; 0<br \/>\ndef add():<br \/>\n    count &#061; 100       # \u521b\u5efa\u7684\u662f\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4e0d\u662f\u5168\u5c40<br \/>\n    print(&#034;\u51fd\u6570\u5185&#xff1a;&#034;, count)<br \/>\nadd()                 # \u51fd\u6570\u5185&#xff1a;100<br \/>\nprint(&#034;\u51fd\u6570\u5916&#xff1a;&#034;, count)   # \u51fd\u6570\u5916&#xff1a;0&#xff08;\u5168\u5c40\u672a\u53d8&#xff09;<\/p>\n<ul>\n<li>\u4e0d\u52a0 global \u4e14\u8d4b\u503c\u524d\u8bfb\u53d6&#xff0c;\u4f1a\u62a5 UnboundLocalError<\/li>\n<\/ul>\n<p>count &#061; 0<br \/>\ndef add():<br \/>\ncount &#043;&#061; 1        # \u672a\u58f0\u660e global&#xff0c;count \u88ab\u5f53\u5c40\u90e8\u53d8\u91cf<br \/>\nadd()<br \/>\nUnboundLocalError: local variable &#039;count&#039; referenced before assignment<\/p>\n<h5>nonlocal&#xff1a;\u5d4c\u5957\u51fd\u6570\u5185&#xff0c;\u5185\u90e8\u51fd\u6570\u4fee\u6539\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<\/h5>\n<ul>\n<li>\u57fa\u672c\u7528\u6cd5<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    count &#061; 0<br \/>\n    def inner():<br \/>\n        nonlocal count    # \u58f0\u660e count \u662f\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<br \/>\n        count &#043;&#061; 1<br \/>\n    inner()<br \/>\n    inner()<br \/>\n    print(count)          # 2<br \/>\nouter()<\/p>\n<ul>\n<li>\u4e0d\u80fd\u7528\u4e8e\u5168\u5c40\u4f5c\u7528\u57df&#xff0c;\u53ea\u80fd\u7528\u4e8e\u5d4c\u5957\u51fd\u6570<\/li>\n<\/ul>\n<p>count &#061; 0<br \/>\ndef f():<br \/>\n    nonlocal count        # \u5168\u5c40\u4f5c\u7528\u57df\u4e0d\u80fd\u7528 nonlocal<br \/>\n# SyntaxError: no binding for nonlocal &#039;count&#039; found<\/p>\n<ul>\n<li>\n<p>\u5916\u5c42\u6ca1\u6709\u5bf9\u5e94\u53d8\u91cf\u65f6\u4e5f\u4f1a\u62a5\u9519<\/p>\n<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    def inner():<br \/>\n        nonlocal x        # \u5916\u5c42\u6ca1\u6709 x<br \/>\n        x &#061; 1  # SyntaxError: no binding for nonlocal &#039;x&#039; found<\/p>\n<ul>\n<li>\u4e0d\u52a0 nonlocal \u65f6&#xff0c;\u5185\u5c42\u8d4b\u503c\u4f1a\u521b\u5efa\u81ea\u5df1\u7684\u5c40\u90e8\u53d8\u91cf<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    count &#061; 0<br \/>\n    def inner():<br \/>\n        count &#061; 100       # \u521b\u5efa\u7684\u662f inner \u7684\u5c40\u90e8\u53d8\u91cf<br \/>\n        print(&#034;inner&#xff1a;&#034;, count)<br \/>\n    inner()<br \/>\n    print(&#034;outer&#xff1a;&#034;, count)   # outer \u7684 count \u6ca1\u53d8<br \/>\nouter()<\/p>\n<ul>\n<li>\u4e0d\u52a0 nonlocal \u4e14\u8d4b\u503c\u524d\u8bfb\u53d6&#xff0c;\u4f1a\u62a5 UnboundLocalError<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    count &#061; 0<br \/>\n    def inner():<br \/>\n        count &#043;&#061; 1        # \u672a\u58f0\u660e nonlocal<br \/>\n    inner()<br \/>\nouter() # UnboundLocalError: local variable &#039;count&#039; referenced before assignment<\/p>\n<h5>global \u548c nonlocal \u53ea\u5f71\u54cd\u8d4b\u503c\u884c\u4e3a&#xff0c;\u8bfb\u53d6\u4e0d\u9700\u8981\u58f0\u660e<\/h5>\n<ul>\n<li>\u8bfb\u53d6\u5168\u5c40\u53d8\u91cf&#xff0c;\u65e0\u9700 global<\/li>\n<\/ul>\n<p>count &#061; 10<br \/>\ndef show():<br \/>\n    print(count)          # \u76f4\u63a5\u8bfb\u53d6&#xff0c;\u9075\u5faa LEGB<br \/>\nshow()                    # 10<\/p>\n<ul>\n<li>\n<p>\u8bfb\u53d6\u5916\u5c42\u51fd\u6570\u53d8\u91cf&#xff0c;\u65e0\u9700 nonlocal<\/p>\n<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    count &#061; 10<br \/>\n    def inner():<br \/>\n        print(count)      # \u76f4\u63a5\u8bfb\u53d6<br \/>\n    inner()<br \/>\nouter()                   # 10<\/p>\n<ul>\n<li>\n<p>\u58f0\u660e\u53ea\u6539\u53d8\u8d4b\u503c\u7684\u76ee\u6807\u4f5c\u7528\u57df<\/p>\n<\/li>\n<\/ul>\n<p>x &#061; &#034;\u5168\u5c40&#034;<br \/>\ndef f():<br \/>\n    print(x)              # \u8bfb\u53d6&#xff1a;\u62ff\u5230\u5168\u5c40 x<br \/>\n    # x &#061; &#034;\u5c40\u90e8&#034;          # \u82e5\u53d6\u6d88\u6ce8\u91ca&#xff0c;x \u53d8\u5c40\u90e8&#xff0c;\u4e0a\u4e00\u884c\u4f1a\u62a5 UnboundLocalError<br \/>\nf()                       # \u5168\u5c40<\/p>\n<h4>4.4.3 global \u4e0e nonlocal \u5bf9\u6bd4<\/h4>\n<p>\u4e0b\u9762\u901a\u8fc7\u5bf9\u6bd4\u8868\u683c\u548c\u793a\u4f8b\u4ee3\u7801&#xff0c;\u68b3\u7406 global \u4e0e nonlocal \u7684\u6838\u5fc3\u533a\u522b&#xff1a;<\/p>\n<table>\n<tr>\u5bf9\u6bd4\u9879globalnonlocal<\/tr>\n<tbody>\n<tr>\n<td>\u4f5c\u7528\u76ee\u6807<\/td>\n<td>\u5168\u5c40\u4f5c\u7528\u57df\u7684\u53d8\u91cf<\/td>\n<td>\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u4f7f\u7528\u4f4d\u7f6e<\/td>\n<td>\u4efb\u610f\u51fd\u6570\u5185\u90e8<\/td>\n<td>\u4ec5\u5d4c\u5957\u51fd\u6570\u5185\u90e8<\/td>\n<\/tr>\n<tr>\n<td>\u53d8\u91cf\u4e0d\u5b58\u5728\u65f6<\/td>\n<td>\u8d4b\u503c\u65f6\u81ea\u52a8\u5728\u5168\u5c40\u521b\u5efa<\/td>\n<td>\u62a5 SyntaxError&#xff08;\u5916\u5c42\u5fc5\u987b\u5df2\u5b58\u5728&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u80fd\u5426\u7528\u4e8e\u5168\u5c40\u4f5c\u7528\u57df<\/td>\n<td>\u53ef\u4ee5&#xff08;\u4f46\u901a\u5e38\u6ca1\u5fc5\u8981&#xff09;<\/td>\n<td>\u4e0d\u80fd&#xff0c;\u4f1a\u62a5 SyntaxError<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u5f71\u54cd\u8d4b\u503c\u884c\u4e3a<\/td>\n<td>\u662f&#xff0c;\u8bfb\u53d6\u65e0\u9700\u58f0\u660e<\/td>\n<td>\u662f&#xff0c;\u8bfb\u53d6\u65e0\u9700\u58f0\u660e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4.4.4 LEGB<\/h4>\n<h5>\u67e5\u627e\u89c4\u5219&#xff1a;\u5c40\u90e8 \u2192 \u5916\u5c42\u51fd\u6570 \u2192 \u5168\u5c40 \u2192 \u5185\u7f6e\u4f5c\u7528\u57df<\/h5>\n<ul>\n<li>\n<p>L&#xff08;Local&#xff09;&#xff1a;\u5c40\u90e8\u4f5c\u7528\u57df&#xff0c;\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>E&#xff08;Enclosing&#xff09;&#xff1a;\u5916\u5c42\u51fd\u6570\u4f5c\u7528\u57df&#xff0c;\u5d4c\u5957\u51fd\u6570\u4e2d\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>G&#xff08;Global&#xff09;&#xff1a;\u5168\u5c40\u4f5c\u7528\u57df&#xff0c;\u6a21\u5757\u7ea7\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>B&#xff08;Built-in&#xff09;&#xff1a;\u5185\u7f6e\u4f5c\u7528\u57df&#xff0c;Python \u5185\u7f6e\u547d\u540d\u7a7a\u95f4&#xff08;\u5982 len\u3001print\u3001sum&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u8bfb\u53d6\u53d8\u91cf\u65f6\u6309 L \u2192 E \u2192 G \u2192 B \u987a\u5e8f\u9010\u5c42\u5411\u4e0a\u67e5\u627e&#xff0c;\u627e\u5230\u5373\u505c<\/p>\n<\/li>\n<\/ul>\n<h5>LEGB \u662f\u8bfb\u53d6\u53d8\u91cf\u65f6\u7684\u67e5\u627e\u987a\u5e8f&#xff0c;\u8d4b\u503c\u65f6\u4e0d\u9002\u7528&#xff08;\u8d4b\u503c\u9ed8\u8ba4\u521b\u5efa\u5c40\u90e8\u53d8\u91cf&#xff09;<\/h5>\n<ul>\n<li>\n<p>\u8bfb\u53d6&#xff1a;\u6309 LEGB \u9010\u5c42\u5411\u4e0a\u627e<\/p>\n<\/li>\n<li>\n<p>\u8d4b\u503c&#xff1a;\u9ed8\u8ba4\u5728\u5f53\u524d\u4f5c\u7528\u57df\u521b\u5efa\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4e0d\u5411\u4e0a\u627e<\/p>\n<\/li>\n<li>\n<p>\u51fd\u6570\u5185\u6709\u8d4b\u503c\u8bed\u53e5\u65f6&#xff0c;\u8be5\u53d8\u91cf\u5728\u6574\u4e2a\u51fd\u6570\u5185\u90fd\u88ab\u89c6\u4e3a\u5c40\u90e8\u53d8\u91cf<\/p>\n<ul>\n<li>\n<p>\u82e5\u8d4b\u503c\u524d\u5c31\u8bfb\u53d6&#xff0c;\u4f1a\u62a5 UnboundLocalError<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u5bf9\u6bd4&#xff1a;\u5220\u9664\u51fd\u6570\u5185\u8d4b\u503c\u8bed\u53e5&#xff0c;\u8bfb\u53d6\u5c31\u80fd\u6b63\u5e38\u5411\u4e0a\u627e\u5230\u5916\u5c42\/\u5168\u5c40\u53d8\u91cf<\/p>\n<\/li>\n<\/ul>\n<h5>global \u548c nonlocal \u4f1a\u6539\u53d8\u8d4b\u503c\u7684\u76ee\u6807\u4f5c\u7528\u57df<\/h5>\n<ul>\n<li>\n<p>global&#xff1a;\u628a\u8d4b\u503c\u76ee\u6807\u6539\u4e3a\u5168\u5c40\u4f5c\u7528\u57df<\/p>\n<\/li>\n<li>\n<p>nonlocal&#xff1a;\u628a\u8d4b\u503c\u76ee\u6807\u6539\u4e3a\u5916\u5c42\u51fd\u6570\u5c40\u90e8\u4f5c\u7528\u57df<\/p>\n<\/li>\n<li>\n<p>\u4e8c\u8005\u53ea\u6539\u53d8\u8d4b\u503c\u7684\u76ee\u6807\u4f5c\u7528\u57df&#xff0c;\u8bfb\u53d6\u4ecd\u81ea\u52a8\u9075\u5faa LEGB<\/p>\n<\/li>\n<li>\n<p>\u9ed8\u8ba4\u8d4b\u503c&#xff08;\u4e0d\u52a0\u58f0\u660e&#xff09;&#xff1a;\u521b\u5efa\u5c40\u90e8\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>\u4e09\u8005\u5bf9\u6bd4&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u9ed8\u8ba4\u8d4b\u503c \u2192 \u5c40\u90e8\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>global \u2192 \u5168\u5c40\u53d8\u91cf<\/p>\n<\/li>\n<li>\n<p>nonlocal \u2192 \u5916\u5c42\u51fd\u6570\u5c40\u90e8\u53d8\u91cf<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>4.5 \u5d4c\u5957\u51fd\u6570<\/h3>\n<ul>\n<li>\u5b9a\u4e49&#xff1a;\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570<\/li>\n<li>\u7279\u6027&#xff1a;\u5185\u5c42\u51fd\u6570\u53ef\u8bfb\u53d6\u5916\u5c42\u51fd\u6570\u53d8\u91cf&#xff1b;\u5916\u5c42\u51fd\u6570\u5916\u90e8\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528\u5185\u5c42\u51fd\u6570<\/li>\n<li>\u5185\u5c42\u51fd\u6570\u9ed8\u8ba4\u53ea\u80fd\u8bfb\u53d6\u5916\u90e8\u51fd\u6570\u53d8\u91cf&#xff0c;\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539&#xff1b;\u4fee\u6539\u9700\u8981nonlocal<\/li>\n<\/ul>\n<p>def outer():<br \/>\n    def inner():<br \/>\n        print(&#034;inner&#034;)<br \/>\n    inner()          # \u5916\u5c42\u5185\u90e8\u53ef\u4ee5\u8c03\u7528<br \/>\nouter()<br \/>\n# inner<br \/>\n# inner()           # \u5916\u5c42\u5916\u90e8\u4e0d\u80fd\u76f4\u63a5\u8c03\u7528<br \/>\n# NameError: name &#039;inner&#039; is not defined<\/p>\n<h3>4.6 lambda \u8868\u8fbe\u5f0f<\/h3>\n<ul>\n<li>\u8bed\u6cd5&#xff1a;lambda \u53c2\u6570\u5217\u8868 : \u8868\u8fbe\u5f0f\/\u8fd4\u56de\u503c;<\/li>\n<\/ul>\n<ul>\n<li>lambda \u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u51fd\u6570\u5bf9\u8c61<\/li>\n<\/ul>\n<h4>4.6.1 \u6ce8\u610f\u70b9<\/h4>\n<ul>\n<li>lambda \u53ea\u80fd\u5305\u542b\u8868\u8fbe\u5f0f&#xff08;\u4e09\u5143\u8868\u8fbe\u5f0f\u7b49&#xff09;&#xff0c;\u4e0d\u80fd\u5305\u542b\u8bed\u53e5&#xff08;\u5982\u8d4b\u503c\u3001\u5faa\u73af\u3001if \u5757&#xff09;<\/li>\n<\/ul>\n<p># \u8868\u8fbe\u5f0f&#xff1a;\u4e09\u5143\u8868\u8fbe\u5f0f\u53ef\u4ee5<br \/>\nf &#061; lambda x: &#034;\u6b63\u6570&#034; if x &gt; 0 else &#034;\u975e\u6b63\u6570&#034;<br \/>\nprint(f(1))      # \u6b63\u6570<br \/>\nprint(f(-1))     # \u975e\u6b63\u6570<br \/>\n# \u8868\u8fbe\u5f0f&#xff1a;\u7b97\u672f\u3001\u6bd4\u8f83\u3001\u51fd\u6570\u8c03\u7528\u90fd\u53ef\u4ee5<br \/>\ng &#061; lambda x: x * 2 &#043; 1<br \/>\nprint(g(3))      # 7<br \/>\n# \u4e0d\u80fd\u5305\u542b\u8d4b\u503c\u8bed\u53e5<br \/>\n# h &#061; lambda x: y &#061; x &#043; 1<br \/>\n# SyntaxError: invalid syntax<br \/>\n# \u4e0d\u80fd\u5305\u542b\u5faa\u73af\u8bed\u53e5<br \/>\n# h &#061; lambda x: for i in range(x): print(i)<br \/>\n# SyntaxError: invalid syntax<br \/>\n# \u4e0d\u80fd\u5305\u542b if \u5757&#xff08;\u6ce8\u610f&#xff1a;\u4e09\u5143\u8868\u8fbe\u5f0f\u4e0d\u662f if \u5757&#xff09;<br \/>\n# h &#061; lambda x: if x &gt; 0: print(&#034;\u6b63&#034;)<br \/>\n# SyntaxError: invalid syntax<\/p>\n<ul>\n<li>lambda \u4e0d\u80fd\u6709 return&#xff0c;\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5c31\u662f\u8fd4\u56de\u503c<\/li>\n<\/ul>\n<p># \u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u81ea\u52a8\u4f5c\u4e3a\u8fd4\u56de\u503c<br \/>\nf &#061; lambda x: x &#043; 1<br \/>\nprint(f(1))      # 2<br \/>\n# \u4e0d\u80fd\u5199 return<br \/>\n# g &#061; lambda x: return x &#043; 1<br \/>\n# SyntaxError: invalid syntax<\/p>\n<ul>\n<li>lambda \u4e0d\u80fd\u6709\u6587\u6863\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n<p># def \u53ef\u4ee5\u6709 docstring<br \/>\ndef f(x):<br \/>\n    &#034;&#034;&#034;\u8fd9\u662f\u8bf4\u660e\u6587\u6863&#034;&#034;&#034;<br \/>\n    return x &#043; 1<br \/>\nprint(f.__doc__)   # \u8fd9\u662f\u8bf4\u660e\u6587\u6863<br \/>\n# lambda \u4e0d\u80fd\u6709 docstring<br \/>\ng &#061; lambda x: x &#043; 1<br \/>\nprint(g.__doc__)   # None<\/p>\n<ul>\n<li>lambda \u53ef\u4ee5\u63a5\u53d7\u6240\u6709\u7c7b\u578b\u7684\u53c2\u6570&#xff08;\u4f4d\u7f6e\u3001\u9ed8\u8ba4\u3001*args\u3001**kwargs&#xff09;<\/li>\n<\/ul>\n<p># \u4f4d\u7f6e\u53c2\u6570<br \/>\nf1 &#061; lambda a, b: a &#043; b<br \/>\nprint(f1(1, 2))              # 3<br \/>\n# \u9ed8\u8ba4\u53c2\u6570<br \/>\nf2 &#061; lambda a, b&#061;10: a &#043; b<br \/>\nprint(f2(1))                 # 11<br \/>\nprint(f2(1, 2))              # 3<br \/>\n# *args<br \/>\nf3 &#061; lambda *args: sum(args)<br \/>\nprint(f3(1, 2, 3))           # 6<br \/>\n# **kwargs<br \/>\nf4 &#061; lambda **kwargs: kwargs<br \/>\nprint(f4(a&#061;1, b&#061;2))          # {&#039;a&#039;: 1, &#039;b&#039;: 2}<br \/>\n# \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff08;\u7528 * \u5206\u9694&#xff09;<br \/>\nf5 &#061; lambda a, *, b: a &#043; b<br \/>\nprint(f5(1, b&#061;2))            # 3<\/p>\n<h4>4.6.2 \u8c03\u7528<\/h4>\n<ul>\n<li>\u76f4\u63a5\u6267\u884c&#xff1a;(lambda \u53c2\u6570\u5217\u8868:\u8868\u8fbe\u5f0f\/\u8fd4\u56de\u503c)(\u53c2\u6570\u5217\u8868)<\/li>\n<\/ul>\n<p>print((lambda x: x &#043; 1)(1))          # 2<br \/>\nprint((lambda a, b: a * b)(2, 3))    # 6<br \/>\nprint((lambda: 100)())               # 100<\/p>\n<ul>\n<li>\u8d4b\u503c\u8c03\u7528&#xff1a;name &#061; lambda \u53c2\u6570\u5217\u8868:\u8868\u8fbe\u5f0f\/\u8fd4\u56de\u503c; name(\u53c2\u6570\u5217\u8868)<\/li>\n<\/ul>\n<p>f &#061; lambda x: x &#043; 1<br \/>\nprint(f(1))          # 2<br \/>\nadd &#061; lambda a, b: a &#043; b<br \/>\nprint(add(2, 3))     # 5<\/p>\n<ul>\n<li>lambda \u5e38\u4e0emap()\u3001filter()\u3001sorted(key&#061;&#8230;)\u7b49\u914d\u5408\u4f7f\u7528<\/li>\n<\/ul>\n<p>nums &#061; [3, 1, 2]<br \/>\n# map&#xff1a;\u5bf9\u6bcf\u4e2a\u5143\u7d20\u505a\u8f6c\u6362<br \/>\nprint(list(map(lambda x: x * 2, nums)))       # [6, 2, 4]<br \/>\n# filter&#xff1a;\u6309\u6761\u4ef6\u7b5b\u9009<br \/>\nprint(list(filter(lambda x: x &gt; 1, nums)))    # [3, 2]<br \/>\n# sorted&#xff1a;\u6309 key \u6392\u5e8f<br \/>\nprint(sorted(nums, key&#061;lambda x: -x))         # [3, 2, 1]<br \/>\n# \u5bf9\u5b57\u5178\u5217\u8868\u6392\u5e8f<br \/>\nusers &#061; [{&#034;name&#034;: &#034;\u5c0f\u660e&#034;, &#034;age&#034;: 18}, {&#034;name&#034;: &#034;\u5c0f\u7ea2&#034;, &#034;age&#034;: 20}]<br \/>\nprint(sorted(users, key&#061;lambda u: u[&#034;age&#034;]))  # \u6309\u5e74\u9f84\u5347\u5e8f<\/p>\n<ul>\n<li>PEP 8 \u5efa\u8bae\u4e0d\u8981\u5c06 lambda \u8d4b\u503c\u7ed9\u53d8\u91cf&#xff0c;\u800c\u5e94\u7528def<\/li>\n<\/ul>\n<p># PEP 8 \u4e0d\u63a8\u8350<br \/>\nf &#061; lambda x: x &#043; 1<br \/>\n# PEP 8 \u63a8\u8350<br \/>\ndef f(x):<br \/>\n    return x &#043; 1<br \/>\nprint(f(1))   # 2<\/p>\n<h3>\u603b\u7ed3\u4e0e\u81ea\u6d4b<\/h3>\n<p>\u672c\u7ae0\u7cfb\u7edf\u8bb2\u89e3\u4e86 Python \u51fd\u6570\u7684\u6838\u5fc3\u77e5\u8bc6&#xff0c;\u4e0b\u9762\u7528 5 \u6761\u8981\u70b9\u505a\u4e00\u6b21\u6574\u4f53\u56de\u987e&#xff0c;\u5e76\u9644\u4e0a 5 \u9053\u81ea\u6d4b\u9898\u5e2e\u52a9\u5de9\u56fa\u6613\u9519\u70b9\u3002<\/p>\n<h4>\u4e00\u3001\u6838\u5fc3\u77e5\u8bc6\u70b9\u603b\u7ed3<\/h4>\n<ul>\n<li>\u53c2\u6570\u5b9a\u4e49\u987a\u5e8f&#xff1a;\u4f4d\u7f6e\u53c2\u6570 &gt; \u9ed8\u8ba4\u53c2\u6570 &gt; *args \u53ef\u53d8\u4f4d\u7f6e &gt; \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570 &gt; **kwargs \u53ef\u53d8\u5173\u952e\u5b57&#xff0c;\u987a\u5e8f\u4e0d\u80fd\u98a0\u5012\u3002<\/li>\n<li>\u9ed8\u8ba4\u53c2\u6570\u9677\u9631&#xff1a;\u9ed8\u8ba4\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u4e00\u6b21\u6027\u521b\u5efa&#xff0c;\u6240\u6709\u8c03\u7528\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61&#xff1b;\u53ef\u53d8\u5bf9\u8c61&#xff08;\u5982\u5217\u8868&#xff09;\u505a\u539f\u5730\u4fee\u6539\u4f1a\u7d2f\u79ef&#xff0c;\u5e94\u4f7f\u7528 None \u4f5c\u4e3a\u9ed8\u8ba4\u503c\u5e76\u5728\u51fd\u6570\u4f53\u5185\u521b\u5efa\u65b0\u5bf9\u8c61\u3002<\/li>\n<li>global \u4e0e nonlocal \u7684\u533a\u522b&#xff1a;global \u4fee\u6539\u5168\u5c40\u53d8\u91cf&#xff0c;\u53d8\u91cf\u4e0d\u5b58\u5728\u65f6\u81ea\u52a8\u5728\u5168\u5c40\u521b\u5efa&#xff1b;nonlocal \u4fee\u6539\u5916\u5c42\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u5916\u5c42\u5fc5\u987b\u5df2\u5b58\u5728&#xff0c;\u5426\u5219\u62a5 SyntaxError\u3002<\/li>\n<li>LEGB \u89c4\u5219&#xff1a;\u8bfb\u53d6\u53d8\u91cf\u65f6\u6309 \u5c40\u90e8&#xff08;Local&#xff09;\u2192 \u5916\u5c42\u51fd\u6570&#xff08;Enclosing&#xff09;\u2192 \u5168\u5c40&#xff08;Global&#xff09;\u2192 \u5185\u7f6e&#xff08;Built-in&#xff09;\u9010\u5c42\u67e5\u627e&#xff1b;\u8d4b\u503c\u9ed8\u8ba4\u521b\u5efa\u5c40\u90e8\u53d8\u91cf&#xff0c;\u4e0d\u5411\u4e0a\u67e5\u627e\u3002<\/li>\n<li>lambda \u9650\u5236&#xff1a;lambda \u53ea\u80fd\u5305\u542b\u8868\u8fbe\u5f0f&#xff0c;\u4e0d\u80fd\u5305\u542b\u8bed\u53e5&#xff08;\u8d4b\u503c\u3001\u5faa\u73af\u3001if \u5757&#xff09;&#xff0c;\u4e0d\u80fd\u6709 return \u548c\u6587\u6863\u5b57\u7b26\u4e32&#xff0c;\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u5c31\u662f\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<h4>\u4e8c\u3001\u81ea\u6d4b\u9898<\/h4>\n<p>\u7b2c 1 \u9898&#xff08;\u53c2\u6570\u7c7b\u578b&#xff09;&#xff1a;\u4ee5\u4e0b\u54ea\u4e2a\u51fd\u6570\u5b9a\u4e49\u662f\u6b63\u786e\u7684&#xff1f;<\/p>\n<ul>\n<li>A. def f(a&#061;1, b):<\/li>\n<li>B. def f(a, b&#061;1, *args, c, **kwargs):<\/li>\n<li>C. def f(**kwargs, *args):<\/li>\n<li>D. def f(a, *args, b&#061;1, **kwargs):<\/li>\n<\/ul>\n<p>\u7b54\u6848&#xff1a;D\u3002A \u4e2d\u9ed8\u8ba4\u53c2\u6570\u4e0d\u80fd\u653e\u5728\u4f4d\u7f6e\u53c2\u6570\u4e4b\u524d&#xff1b;B \u4e2d\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570 c \u6ca1\u6709\u9ed8\u8ba4\u503c&#xff0c;\u5fc5\u987b\u653e\u5728 *args \u4e4b\u540e\u4e14 **kwargs \u4e4b\u524d&#xff0c;\u4f46 c \u65e0\u9ed8\u8ba4\u503c\u4f1a\u5bfc\u81f4\u8c03\u7528\u65f6\u5fc5\u987b\u4f20\u5173\u952e\u5b57&#xff0c;\u5b9a\u4e49\u672c\u8eab\u8bed\u6cd5\u6b63\u786e\u4f46 c \u5fc5\u987b\u4f20\u53c2&#xff0c;\u800c D \u4e2d b&#061;1 \u6709\u9ed8\u8ba4\u503c\u66f4\u5408\u7406&#xff1b;C \u4e2d **kwargs \u5fc5\u987b\u653e\u5728\u6700\u540e\u3002D \u5b8c\u5168\u7b26\u5408\u53c2\u6570\u5b9a\u4e49\u987a\u5e8f\u3002<\/p>\n<p>\u7b2c 2 \u9898&#xff08;\u9ed8\u8ba4\u53c2\u6570\u9677\u9631&#xff09;&#xff1a;\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801&#xff0c;\u8f93\u51fa\u7ed3\u679c\u662f&#xff1f;<\/p>\n<p>def add_item(item, lst&#061;[]):<br \/>\n    lst.append(item)<br \/>\n    return lst<br \/>\nprint(add_item(&#034;a&#034;))<br \/>\nprint(add_item(&#034;b&#034;))<\/p>\n<ul>\n<li>A. [&#039;a&#039;] \u548c [&#039;b&#039;]<\/li>\n<li>B. [&#039;a&#039;] \u548c [&#039;a&#039;, &#039;b&#039;]<\/li>\n<li>C. [&#039;a&#039;] \u548c [&#039;a&#039;]<\/li>\n<li>D. \u62a5\u9519<\/li>\n<\/ul>\n<p>\u7b54\u6848&#xff1a;B\u3002\u9ed8\u8ba4\u5217\u8868 [] \u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u521b\u5efa\u4e00\u6b21&#xff0c;\u6240\u6709\u8c03\u7528\u5171\u4eab\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002\u7b2c\u4e00\u6b21\u8c03\u7528 append(&#034;a&#034;) \u540e\u5217\u8868\u53d8\u4e3a [&#039;a&#039;]&#xff0c;\u7b2c\u4e8c\u6b21\u8c03\u7528\u65f6\u9ed8\u8ba4\u5217\u8868\u5df2\u7ecf\u662f [&#039;a&#039;]&#xff0c;\u518d append(&#034;b&#034;) \u5f97\u5230 [&#039;a&#039;, &#039;b&#039;]\u3002\u8fd9\u6b63\u662f\u9ed8\u8ba4\u53c2\u6570\u9677\u9631\u7684\u5178\u578b\u8868\u73b0\u3002<\/p>\n<p>\u7b2c 3 \u9898&#xff08;\u4f5c\u7528\u57df&#xff09;&#xff1a;\u6267\u884c\u4ee5\u4e0b\u4ee3\u7801&#xff0c;\u8f93\u51fa\u7ed3\u679c\u662f&#xff1f;<\/p>\n<p>count &#061; 0<br \/>\ndef add():<br \/>\ncount &#043;&#061; 1<br \/>\nadd()<br \/>\nprint(count)<\/p>\n<ul>\n<li>A. 1<\/li>\n<li>B. 0<\/li>\n<li>C. UnboundLocalError<\/li>\n<li>D. NameError<\/li>\n<\/ul>\n<p>\u7b54\u6848&#xff1a;C\u3002\u51fd\u6570 add \u5185\u90e8\u6709\u8d4b\u503c\u8bed\u53e5 count &#043;&#061; 1&#xff0c;Python \u4f1a\u628a count \u89c6\u4e3a\u5c40\u90e8\u53d8\u91cf\u3002\u7531\u4e8e\u672a\u58f0\u660e global count&#xff0c;\u8d4b\u503c\u524d\u8bfb\u53d6 count \u4f1a\u62a5 UnboundLocalError: local variable &#039;count&#039; referenced before assignment\u3002<\/p>\n<p>\u7b2c 4 \u9898&#xff08;global \u4e0e nonlocal&#xff09;&#xff1a;\u4ee5\u4e0b\u5173\u4e8e global \u548c nonlocal \u7684\u8bf4\u6cd5&#xff0c;\u6b63\u786e\u7684\u662f&#xff1f;<\/p>\n<ul>\n<li>A. nonlocal \u53ef\u4ee5\u7528\u4e8e\u5168\u5c40\u4f5c\u7528\u57df<\/li>\n<li>B. global \u58f0\u660e\u540e&#xff0c;\u53d8\u91cf\u4e0d\u5b58\u5728\u65f6\u8d4b\u503c\u4f1a\u62a5\u9519<\/li>\n<li>C. nonlocal \u53ea\u80fd\u7528\u4e8e\u5d4c\u5957\u51fd\u6570\u5185\u90e8&#xff0c;\u4e14\u5916\u5c42\u5fc5\u987b\u5df2\u5b58\u5728\u5bf9\u5e94\u53d8\u91cf<\/li>\n<li>D. global \u548c nonlocal \u90fd\u5f71\u54cd\u53d8\u91cf\u7684\u8bfb\u53d6\u884c\u4e3a<\/li>\n<\/ul>\n<p>\u7b54\u6848&#xff1a;C\u3002nonlocal \u53ea\u80fd\u7528\u4e8e\u5d4c\u5957\u51fd\u6570\u5185\u90e8&#xff0c;\u4e14\u5916\u5c42\u5fc5\u987b\u5df2\u5b58\u5728\u5bf9\u5e94\u53d8\u91cf&#xff0c;\u5426\u5219\u62a5 SyntaxError\u3002A \u9519\u8bef&#xff0c;nonlocal \u4e0d\u80fd\u7528\u4e8e\u5168\u5c40\u4f5c\u7528\u57df&#xff1b;B \u9519\u8bef&#xff0c;global \u58f0\u660e\u540e\u53d8\u91cf\u4e0d\u5b58\u5728\u65f6\u8d4b\u503c\u4f1a\u81ea\u52a8\u5728\u5168\u5c40\u521b\u5efa&#xff1b;D \u9519\u8bef&#xff0c;\u4e8c\u8005\u53ea\u5f71\u54cd\u8d4b\u503c\u7684\u76ee\u6807\u4f5c\u7528\u57df&#xff0c;\u8bfb\u53d6\u4ecd\u9075\u5faa LEGB\u3002<\/p>\n<p>\u7b2c 5 \u9898&#xff08;lambda&#xff09;&#xff1a;\u4ee5\u4e0b\u54ea\u4e2a lambda \u8868\u8fbe\u5f0f\u662f\u5408\u6cd5\u7684&#xff1f;<\/p>\n<ul>\n<li>A. f &#061; lambda x: return x &#043; 1<\/li>\n<li>B. f &#061; lambda x: x &#043; 1<\/li>\n<li>C. f &#061; lambda x: y &#061; x &#043; 1<\/li>\n<li>D. f &#061; lambda x: if x &gt; 0: print(x)<\/li>\n<\/ul>\n<p>\u7b54\u6848&#xff1a;B\u3002lambda \u53ea\u80fd\u5305\u542b\u8868\u8fbe\u5f0f&#xff0c;\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u81ea\u52a8\u4f5c\u4e3a\u8fd4\u56de\u503c\u3002A \u4e2d lambda \u4e0d\u80fd\u5199 return&#xff1b;C \u4e2d\u5305\u542b\u8d4b\u503c\u8bed\u53e5&#xff0c;\u4e0d\u5408\u6cd5&#xff1b;D \u4e2d\u5305\u542b if \u5757&#xff0c;\u4e0d\u5408\u6cd5&#xff08;\u4e09\u5143\u8868\u8fbe\u5f0f x if \u6761\u4ef6 else y \u624d\u662f\u5408\u6cd5\u7684\u8868\u8fbe\u5f0f&#xff09;\u3002<\/p>\n<\/p>\n<h3>\u7efc\u5408\u5b9e\u6218\u793a\u4f8b<\/h3>\n<p>\u4e0b\u9762\u7ed9\u51fa\u4e24\u4e2a\u7efc\u5408\u5b9e\u6218\u6848\u4f8b&#xff0c;\u628a\u7b2c\u56db\u7ae0\u7684\u6838\u5fc3\u77e5\u8bc6\u70b9\u4e32\u8d77\u6765<\/p>\n<hr \/>\n<h4>\u7efc\u5408\u5b9e\u6218\u4e00&#xff1a;\u5b66\u751f\u6210\u7ee9\u7ba1\u7406\u7cfb\u7edf<\/h4>\n<h5>\u9700\u6c42<\/h5>\n<ul>\n<li>\n<p>\u7528\u51fd\u6570\u7ba1\u7406\u5b66\u751f\u6210\u7ee9<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u6dfb\u52a0\u5b66\u751f&#xff08;\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u7edf\u8ba1\u6210\u7ee9&#xff08;\u591a\u8fd4\u56de\u503c\u3001*args&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u6392\u5e8f&#xff08;lambda &#043; sorted&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5168\u5c40\u8ba1\u6570\u5668\u7edf\u8ba1\u64cd\u4f5c\u6b21\u6570&#xff08;global&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h5>\u5b8c\u6574\u4ee3\u7801<\/h5>\n<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u5168\u5c40\u53d8\u91cf &#061;&#061;&#061;&#061;&#061;<br \/>\nop_count &#061; 0          # \u64cd\u4f5c\u8ba1\u6570<br \/>\nstudents &#061; []         # \u5b66\u751f\u5217\u8868<\/p>\n<p>def add_student(name, score, *, class_name&#061;&#034;\u4e00\u73ed&#034;, **extra):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u6dfb\u52a0\u5b66\u751f<br \/>\n    name       : \u4f4d\u7f6e\u53c2\u6570<br \/>\n    score      : \u4f4d\u7f6e\u53c2\u6570<br \/>\n    class_name : \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff08;\u5e26\u9ed8\u8ba4\u503c&#xff09;<br \/>\n    extra      : \u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570<br \/>\n    &#034;&#034;&#034;<br \/>\n    global op_count          # \u4fee\u6539\u5168\u5c40\u8ba1\u6570\u5668<br \/>\n    op_count &#043;&#061; 1<\/p>\n<p>    student &#061; {<br \/>\n        &#034;name&#034;: name,<br \/>\n        &#034;score&#034;: score,<br \/>\n        &#034;class&#034;: class_name,<br \/>\n    }<br \/>\n    student.update(extra)    # \u5408\u5e76\u989d\u5916\u4fe1\u606f<br \/>\n    students.append(student)<br \/>\n    return student           # \u8fd4\u56de\u65b0\u589e\u5b66\u751f<\/p>\n<p>def score_stats(*scores):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u7edf\u8ba1\u6210\u7ee9&#xff1a;\u6700\u9ad8\u3001\u6700\u4f4e\u3001\u5e73\u5747<br \/>\n    *scores : \u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570<br \/>\n    \u8fd4\u56de    : \u591a\u8fd4\u56de\u503c&#xff08;\u672c\u8d28\u662f\u5143\u7ec4&#xff09;<br \/>\n    &#034;&#034;&#034;<br \/>\n    if not scores:<br \/>\n        return None, None, None<\/p>\n<p>    highest &#061; max(scores)<br \/>\n    lowest &#061; min(scores)<br \/>\n    average &#061; sum(scores) \/ len(scores)<br \/>\n    return highest, lowest, average    # \u591a\u8fd4\u56de\u503c<\/p>\n<p>def rank_students(by&#061;&#034;score&#034;, reverse&#061;True):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u6309\u6307\u5b9a\u5b57\u6bb5\u6392\u5e8f<br \/>\n    by      : \u9ed8\u8ba4\u53c2\u6570<br \/>\n    reverse : \u9ed8\u8ba4\u53c2\u6570<br \/>\n    &#034;&#034;&#034;<br \/>\n    return sorted(students, key&#061;lambda s: s[by], reverse&#061;reverse)<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u8c03\u7528 &#061;&#061;&#061;&#061;&#061;<\/p>\n<p># \u2460 \u6dfb\u52a0\u5b66\u751f&#xff1a;\u4f4d\u7f6e\u53c2\u6570 &#043; \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570 &#043; \u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570<br \/>\nadd_student(&#034;\u5c0f\u660e&#034;, 92)<br \/>\nadd_student(&#034;\u5c0f\u7ea2&#034;, 88, class_name&#061;&#034;\u4e8c\u73ed&#034;)<br \/>\nadd_student(&#034;\u5c0f\u521a&#034;, 95, class_name&#061;&#034;\u4e00\u73ed&#034;, age&#061;18, gender&#061;&#034;\u7537&#034;)<\/p>\n<p># \u2461 \u591a\u8fd4\u56de\u503c&#xff1a;\u7528\u591a\u4e2a\u53d8\u91cf\u63a5\u6536<br \/>\nh, l, avg &#061; score_stats(92, 88, 95)<br \/>\nprint(f&#034;\u6700\u9ad8&#xff1a;{h}&#xff0c;\u6700\u4f4e&#xff1a;{l}&#xff0c;\u5e73\u5747&#xff1a;{avg:.1f}&#034;)<br \/>\n# \u6700\u9ad8&#xff1a;95&#xff0c;\u6700\u4f4e&#xff1a;88&#xff0c;\u5e73\u5747&#xff1a;91.7<\/p>\n<p># \u2462 \u7528 _ \u5ffd\u7565\u4e0d\u9700\u8981\u7684\u8fd4\u56de\u503c<br \/>\nh, _, _ &#061; score_stats(92, 88, 95)<br \/>\nprint(f&#034;\u6700\u9ad8\u5206&#xff1a;{h}&#034;)           # \u6700\u9ad8\u5206&#xff1a;95<\/p>\n<p># \u2463 lambda &#043; sorted \u6392\u5e8f<br \/>\nranked &#061; rank_students(by&#061;&#034;score&#034;, reverse&#061;True)<br \/>\nfor s in ranked:<br \/>\n    print(f&#034;{s[&#039;name&#039;]}&#xff08;{s[&#039;class&#039;]}&#xff09;&#xff1a;{s[&#039;score&#039;]}&#034;)<br \/>\n# \u5c0f\u521a&#xff08;\u4e00\u73ed&#xff09;&#xff1a;95<br \/>\n# \u5c0f\u660e&#xff08;\u4e00\u73ed&#xff09;&#xff1a;92<br \/>\n# \u5c0f\u7ea2&#xff08;\u4e8c\u73ed&#xff09;&#xff1a;88<\/p>\n<p># \u2464 \u5168\u5c40\u8ba1\u6570\u5668<br \/>\nprint(f&#034;\u5171\u6267\u884c\u6dfb\u52a0\u64cd\u4f5c {op_count} \u6b21&#034;)   # \u5171\u6267\u884c\u6dfb\u52a0\u64cd\u4f5c 3 \u6b21<\/p>\n<h4><\/h4>\n<h4>\u7efc\u5408\u5b9e\u6218\u4e8c&#xff1a;\u8d2d\u7269\u8f66\u7ed3\u7b97\u7cfb\u7edf<\/h4>\n<h5>\u9700\u6c42<\/h5>\n<ul>\n<li>\n<p>\u7528\u51fd\u6570\u8ba1\u7b97\u8d2d\u7269\u8f66\u603b\u4ef7<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u5546\u54c1\u6298\u6263&#xff08;\u9ed8\u8ba4\u53c2\u6570&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u4efb\u610f\u6570\u91cf\u5546\u54c1&#xff08;*args&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u6ee1\u51cf\u3001\u4f18\u60e0\u5238\u7b49\u989d\u5916\u89c4\u5219&#xff08;**kwargs&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u7edf\u8ba1\u591a\u4e2a\u8ba2\u5355&#xff08;\u591a\u8fd4\u56de\u503c&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301\u6309\u4ef7\u683c\u6392\u5e8f&#xff08;lambda&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h5>\u5b8c\u6574\u4ee3\u7801<\/h5>\n<p># &#061;&#061;&#061;&#061;&#061; \u5168\u5c40\u53d8\u91cf &#061;&#061;&#061;&#061;&#061;<br \/>\ntotal_orders &#061; 0          # \u8ba2\u5355\u603b\u6570<\/p>\n<p>def calc_item_price(name, price, *, discount&#061;1.0):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u8ba1\u7b97\u5355\u4e2a\u5546\u54c1\u6298\u540e\u4ef7<br \/>\n    name     : \u4f4d\u7f6e\u53c2\u6570<br \/>\n    price    : \u4f4d\u7f6e\u53c2\u6570<br \/>\n    discount : \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff08;\u9ed8\u8ba4\u4e0d\u6253\u6298&#xff09;<br \/>\n    &#034;&#034;&#034;<br \/>\n    return name, price * discount<\/p>\n<p>def checkout(*items, coupon&#061;0, **rules):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u7ed3\u7b97\u8d2d\u7269\u8f66<br \/>\n    *items : \u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570&#xff0c;\u6bcf\u4e2a\u5143\u7d20\u662f (\u540d\u79f0, \u4ef7\u683c)<br \/>\n    coupon : \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff0c;\u4f18\u60e0\u5238\u91d1\u989d<br \/>\n    rules  : \u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570&#xff0c;\u5982 free_shipping&#061;True<br \/>\n    \u8fd4\u56de   : \u591a\u8fd4\u56de\u503c&#xff08;\u539f\u4ef7\u3001\u5b9e\u4ed8\u3001\u5546\u54c1\u6570&#xff09;<br \/>\n    &#034;&#034;&#034;<br \/>\n    global total_orders<br \/>\n    total_orders &#043;&#061; 1<\/p>\n<p>    original &#061; 0<br \/>\n    for name, price in items:<br \/>\n        original &#043;&#061; price<\/p>\n<p>    # \u6ee1\u51cf\u89c4\u5219<br \/>\n    if rules.get(&#034;full_reduce&#034;):<br \/>\n        threshold, reduce &#061; rules[&#034;full_reduce&#034;]<br \/>\n        if original &gt;&#061; threshold:<br \/>\n            original -&#061; reduce<\/p>\n<p>    # \u4f18\u60e0\u5238<br \/>\n    final &#061; original &#8211; coupon<br \/>\n    if final &lt; 0:<br \/>\n        final &#061; 0<\/p>\n<p>    return original, final, len(items)    # \u591a\u8fd4\u56de\u503c<\/p>\n<p>def rank_by_price(*items, reverse&#061;True):<br \/>\n    &#034;&#034;&#034;\u6309\u4ef7\u683c\u6392\u5e8f\u5546\u54c1&#034;&#034;&#034;<br \/>\n    return sorted(items, key&#061;lambda x: x[1], reverse&#061;reverse)<\/p>\n<p># &#061;&#061;&#061;&#061;&#061; \u8c03\u7528 &#061;&#061;&#061;&#061;&#061;<\/p>\n<p># \u2460 \u5355\u5546\u54c1\u6298\u540e\u4ef7<br \/>\nname, final_price &#061; calc_item_price(&#034;\u624b\u673a&#034;, 3000, discount&#061;0.9)<br \/>\nprint(f&#034;{name} \u6298\u540e\u4ef7&#xff1a;{final_price}&#034;)   # \u624b\u673a \u6298\u540e\u4ef7&#xff1a;2700.0<\/p>\n<p># \u2461 \u7ed3\u7b97&#xff1a;\u53ef\u53d8\u4f4d\u7f6e &#043; \u4ec5\u9650\u5173\u952e\u5b57 &#043; \u53ef\u53d8\u5173\u952e\u5b57<br \/>\noriginal, final, count &#061; checkout(<br \/>\n    (&#034;\u624b\u673a&#034;, 3000),<br \/>\n    (&#034;\u8033\u673a&#034;, 500),<br \/>\n    (&#034;\u5145\u7535\u5668&#034;, 100),<br \/>\n    coupon&#061;200,<br \/>\n    full_reduce&#061;(3000, 300),<br \/>\n)<br \/>\nprint(f&#034;\u539f\u4ef7&#xff1a;{original}&#xff0c;\u5b9e\u4ed8&#xff1a;{final}&#xff0c;\u5546\u54c1\u6570&#xff1a;{count}&#034;)<br \/>\n# \u539f\u4ef7&#xff1a;3300&#xff0c;\u5b9e\u4ed8&#xff1a;2800&#xff0c;\u5546\u54c1\u6570&#xff1a;3<\/p>\n<p># \u2462 \u7528 _ \u5ffd\u7565\u5546\u54c1\u6570<br \/>\noriginal, final, _ &#061; checkout((&#034;\u4e66&#034;, 50), coupon&#061;10)<br \/>\nprint(f&#034;\u5b9e\u4ed8&#xff1a;{final}&#034;)                  # \u5b9e\u4ed8&#xff1a;40<\/p>\n<p># \u2463 lambda &#043; sorted \u6392\u5e8f<br \/>\nitems &#061; [(&#034;\u624b\u673a&#034;, 3000), (&#034;\u8033\u673a&#034;, 500), (&#034;\u5145\u7535\u5668&#034;, 100)]<br \/>\nfor n, p in rank_by_price(*items):<br \/>\n    print(f&#034;{n}&#xff1a;{p}&#034;)<br \/>\n# \u624b\u673a&#xff1a;3000<br \/>\n# \u8033\u673a&#xff1a;500<br \/>\n# \u5145\u7535\u5668&#xff1a;100<\/p>\n<p># \u2464 \u5168\u5c40\u8ba1\u6570\u5668<br \/>\nprint(f&#034;\u5171\u7ed3\u7b97 {total_orders} \u5355&#034;)       # \u5171\u7ed3\u7b97 2 \u5355<\/p>\n<h4>\u5efa\u8bae\u7ec3\u4e60<\/h4>\n<p>\u628a\u4e0a\u9762\u4e24\u4e2a\u6848\u4f8b\u81ea\u5df1\u6572\u4e00\u904d&#xff0c;\u7136\u540e\u5c1d\u8bd5\u4ee5\u4e0b\u6539\u9020&#xff1a;<\/p>\n<li>\n<p>\u6848\u4f8b\u4e00&#xff1a;\u7ed9 rank_students \u589e\u52a0\u4e00\u4e2a top_n&#061;3 \u53c2\u6570&#xff0c;\u53ea\u8fd4\u56de\u524d N \u540d\u3002<\/p>\n<\/li>\n<li>\n<p>\u6848\u4f8b\u4e8c&#xff1a;\u7ed9 checkout \u589e\u52a0\u4e00\u4e2a tax_rate&#061;0.0 \u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff0c;\u8ba1\u7b97\u542b\u7a0e\u4ef7\u3002<\/p>\n<\/li>\n<li>\n<p>\u7efc\u5408&#xff1a;\u7528 lambda \u548c sorted \u5b9e\u73b0\u4e00\u4e2a&#034;\u6309\u591a\u4e2a\u5b57\u6bb5\u6392\u5e8f&#034;\u7684\u51fd\u6570&#xff0c;\u4f8b\u5982\u5148\u6309\u73ed\u7ea7\u3001\u518d\u6309\u6210\u7ee9\u3002<\/p>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3 Python \u51fd\u6570\u7684\u5b8c\u6574\u77e5\u8bc6\u4f53\u7cfb&#xff0c;\u6db5\u76d6\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5\u3001\u5404\u7c7b\u53c2\u6570\u7c7b\u578b&#xff08;\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff09;\u53ca\u5176\u5b9a\u4e49\u987a\u5e8f\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f5c\u7528\u57df&#xff08;\u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf\u3001global \u4e0e nonlocal \u7684\u5bf9\u6bd4\u3001LEGB \u67e5\u627e\u89c4\u5219&#xff09;\u3001\u5d4c\u5957\u51fd\u6570\u4ee5\u53ca lambda \u8868\u8fbe\u5f0f\u3002\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u5747\u914d\u6709\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u6613\u9519\u70b9\u603b\u7ed3&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 Python \u51fd\u6570<\/p>\n","protected":false},"author":2,"featured_media":107400,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,371,190],"topic":[],"class_list":["post-107401","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-python","tag-371","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/107401.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3 Python \u51fd\u6570\u7684\u5b8c\u6574\u77e5\u8bc6\u4f53\u7cfb&#xff0c;\u6db5\u76d6\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5\u3001\u5404\u7c7b\u53c2\u6570\u7c7b\u578b&#xff08;\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff09;\u53ca\u5176\u5b9a\u4e49\u987a\u5e8f\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f5c\u7528\u57df&#xff08;\u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf\u3001global \u4e0e nonlocal \u7684\u5bf9\u6bd4\u3001LEGB \u67e5\u627e\u89c4\u5219&#xff09;\u3001\u5d4c\u5957\u51fd\u6570\u4ee5\u53ca lambda \u8868\u8fbe\u5f0f\u3002\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u5747\u914d\u6709\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u6613\u9519\u70b9\u603b\u7ed3&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 Python \u51fd\u6570\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/107401.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-19T08:56:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919085642-6aae4e4aad608.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107401.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/107401.html\",\"name\":\"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-19T08:56:46+00:00\",\"dateModified\":\"2026-09-19T08:56:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/107401.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/107401.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107401.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/107401.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6458\u8981&#xff1a;\u672c\u6587\u7cfb\u7edf\u8bb2\u89e3 Python \u51fd\u6570\u7684\u5b8c\u6574\u77e5\u8bc6\u4f53\u7cfb&#xff0c;\u6db5\u76d6\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5\u3001\u5404\u7c7b\u53c2\u6570\u7c7b\u578b&#xff08;\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u4ec5\u9650\u5173\u952e\u5b57\u53c2\u6570&#xff09;\u53ca\u5176\u5b9a\u4e49\u987a\u5e8f\u3001\u8fd4\u56de\u503c\u3001\u51fd\u6570\u4f5c\u7528\u57df&#xff08;\u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf\u3001global \u4e0e nonlocal \u7684\u5bf9\u6bd4\u3001LEGB \u67e5\u627e\u89c4\u5219&#xff09;\u3001\u5d4c\u5957\u51fd\u6570\u4ee5\u53ca lambda \u8868\u8fbe\u5f0f\u3002\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u5747\u914d\u6709\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\u4ee3\u7801\u548c\u6613\u9519\u70b9\u603b\u7ed3&#xff0c;\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 Python \u51fd\u6570","og_url":"https:\/\/www.wsisp.com\/helps\/107401.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-19T08:56:46+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919085642-6aae4e4aad608.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"13 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/107401.html","url":"https:\/\/www.wsisp.com\/helps\/107401.html","name":"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-19T08:56:46+00:00","dateModified":"2026-09-19T08:56:46+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/107401.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/107401.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/107401.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5927\u6a21\u578b\u5b66\u4e60\u4e4bpython\u57fa\u7840\u2014\u2014\u51fd\u6570"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=107401"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107401\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/107400"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=107401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=107401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=107401"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=107401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}