{"id":1159988,"date":"2025-01-13T18:59:37","date_gmt":"2025-01-13T10:59:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159988.html"},"modified":"2025-01-13T18:59:41","modified_gmt":"2025-01-13T10:59:41","slug":"python%e9%97%ad%e5%8c%85%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159988.html","title":{"rendered":"python\u95ed\u5305\u51fd\u6570\u5982\u4f55\u6267\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201600\/8ece96fa-d5ae-4eaa-a9d3-b914bf59c89e.webp\" alt=\"python\u95ed\u5305\u51fd\u6570\u5982\u4f55\u6267\u884c\" \/><\/p>\n<p><p> <strong>Python\u95ed\u5305\u51fd\u6570\u6267\u884c\u7684\u6838\u5fc3\u5728\u4e8e\uff1a\u51fd\u6570\u5d4c\u5957\u3001\u81ea\u7531\u53d8\u91cf\u3001\u8fd4\u56de\u5185\u5c42\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c\u81ea\u7531\u53d8\u91cf\u7684\u6982\u5ff5\u5c24\u4e3a\u91cd\u8981\uff0c\u81ea\u7531\u53d8\u91cf\u662f\u5728\u5185\u5c42\u51fd\u6570\u4e2d\u4f7f\u7528\u4f46\u672a\u5728\u8be5\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u95ed\u5305\u51fd\u6570\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u5bf9\u5916\u5c42\u51fd\u6570\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u3002<strong>\u81ea\u7531\u53d8\u91cf\u5728\u95ed\u5305\u51fd\u6570\u4e2d\u5145\u5f53\u6865\u6881\uff0c\u901a\u8fc7\u5b83\u5b9e\u73b0\u4e86\u5185\u5c42\u51fd\u6570\u5bf9\u5916\u5c42\u51fd\u6570\u73af\u5883\u7684\u6355\u83b7<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p>closure = outer_func(10)<\/p>\n<p>result = closure(5)  # \u7ed3\u679c\u4e3a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_func<\/code>\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u6355\u83b7\u4e86<code>outer_func<\/code>\u7684\u5c40\u90e8\u53d8\u91cf<code>x<\/code>\u3002\u5373\u4f7f<code>outer_func<\/code>\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u5e76\u8fd4\u56de\uff0c<code>inner_func<\/code>\u4f9d\u7136\u80fd\u591f\u8bbf\u95ee\u5e76\u4f7f\u7528<code>x<\/code>\u7684\u503c\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u89e3\u91ca\u95ed\u5305\u51fd\u6570\u7684\u6267\u884c\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u95ed\u5305\u7684\u5b9a\u4e49\u548c\u57fa\u672c\u7ed3\u6784<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u95ed\u5305<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u5916\u90e8\u51fd\u6570\u5c06\u5185\u90e8\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\u540e\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u7684\u6267\u884c\u73af\u5883\u5df2\u7ecf\u9500\u6bc1\uff0c\u5185\u90e8\u51fd\u6570\u4f9d\u7136\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_func<\/code>\u5c31\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u56e0\u4e3a\u5b83\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570<code>outer_func<\/code>\u4e2d\u7684\u53d8\u91cf<code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u95ed\u5305\u7684\u57fa\u672c\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u901a\u5e38\u7531\u4e09\u4e2a\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5916\u5c42\u51fd\u6570<\/li>\n<li>\u5185\u5c42\u51fd\u6570<\/li>\n<li>\u81ea\u7531\u53d8\u91cf<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5916\u5c42\u51fd\u6570\u662f<code>outer_func<\/code><\/li>\n<li>\u5185\u5c42\u51fd\u6570\u662f<code>inner_func<\/code><\/li>\n<li>\u81ea\u7531\u53d8\u91cf\u662f<code>x<\/code><\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u81ea\u7531\u53d8\u91cf\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u81ea\u7531\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u81ea\u7531\u53d8\u91cf\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u4f7f\u7528\uff0c\u4f46\u65e2\u4e0d\u662f\u8be5\u51fd\u6570\u53c2\u6570\uff0c\u4e5f\u4e0d\u662f\u8be5\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002\u5728\u95ed\u5305\u4e2d\uff0c\u81ea\u7531\u53d8\u91cf\u901a\u5e38\u662f\u5916\u5c42\u51fd\u6570\u7684\u53c2\u6570\u6216\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code>\u5c31\u662f<code>inner_func<\/code>\u7684\u81ea\u7531\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u81ea\u7531\u53d8\u91cf\u7684\u6355\u83b7<\/h4>\n<\/p>\n<p><p>\u5f53\u95ed\u5305\u6355\u83b7\u81ea\u7531\u53d8\u91cf\u65f6\uff0c\u8fd9\u4e9b\u53d8\u91cf\u7684\u503c\u4f1a\u4fdd\u5b58\u5728\u5185\u5c42\u51fd\u6570\u7684\u73af\u5883\u4e2d\u3002\u5373\u4f7f\u5916\u5c42\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\uff0c\u5185\u5c42\u51fd\u6570\u4f9d\u7136\u53ef\u4ee5\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">closure = outer_func(10)<\/p>\n<p>result = closure(5)  # \u7ed3\u679c\u4e3a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>closure<\/code>\u662f<code>inner_func<\/code>\u7684\u5b9e\u4f8b\uff0c\u5b83\u6355\u83b7\u4e86<code>x<\/code>\u7684\u503c<code>10<\/code>\uff0c\u5e76\u5728\u540e\u7eed\u6267\u884c\u4e2d\u4f7f\u7528\u4e86\u8fd9\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u95ed\u5305\u7684\u6267\u884c\u8fc7\u7a0b<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u9636\u6bb5<\/h4>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u95ed\u5305\u65f6\uff0c\u5916\u5c42\u51fd\u6570\u548c\u5185\u5c42\u51fd\u6570\u90fd\u4f1a\u88ab\u5b9a\u4e49\uff0c\u4f46\u5185\u5c42\u51fd\u6570\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func(x):<\/p>\n<p>    def inner_func(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_func<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>outer_func<\/code>\u548c<code>inner_func<\/code>\u90fd\u88ab\u5b9a\u4e49\uff0c\u4f46<code>inner_func<\/code>\u4e0d\u4f1a\u7acb\u5373\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8c03\u7528\u5916\u5c42\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u8c03\u7528\u5916\u5c42\u51fd\u6570\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\uff0c\u5e76\u6355\u83b7\u5916\u5c42\u51fd\u6570\u7684\u73af\u5883\uff0c\u5305\u62ec\u81ea\u7531\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">closure = outer_func(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528<code>outer_func(10)<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61<code>inner_func<\/code>\uff0c\u5e76\u6355\u83b7<code>x<\/code>\u7684\u503c<code>10<\/code>\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u8c03\u7528\u5185\u5c42\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u8c03\u7528\u5185\u5c42\u51fd\u6570\u65f6\uff0c\u5b83\u4f1a\u4f7f\u7528\u6355\u83b7\u7684\u81ea\u7531\u53d8\u91cf\u8fdb\u884c\u8ba1\u7b97\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = closure(5)  # \u7ed3\u679c\u4e3a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u8c03\u7528<code>closure(5)<\/code>\u65f6\uff0c\u5185\u5c42\u51fd\u6570<code>inner_func<\/code>\u4f7f\u7528\u6355\u83b7\u7684\u81ea\u7531\u53d8\u91cf<code>x<\/code>\uff08\u503c\u4e3a10\uff09\u548c\u53c2\u6570<code>y<\/code>\uff08\u503c\u4e3a5\uff09\u8fdb\u884c\u8ba1\u7b97\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u95ed\u5305\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u5728Python\u4e2d\u6709\u8bb8\u591a\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5ef6\u8fdf\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u5373\u5728\u9700\u8981\u65f6\u624d\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_multiplier(x):<\/p>\n<p>    def multiplier(y):<\/p>\n<p>        return x * y<\/p>\n<p>    return multiplier<\/p>\n<p>times_3 = make_multiplier(3)<\/p>\n<p>result = times_3(10)  # \u7ed3\u679c\u4e3a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_multiplier<\/code>\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305<code>multiplier<\/code>\uff0c\u5b83\u6355\u83b7\u4e86<code>x<\/code>\u7684\u503c\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u4e58\u6cd5\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4fdd\u6301\u72b6\u6001<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u72b6\u6001\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_counter():<\/p>\n<p>    count = 0<\/p>\n<p>    def counter():<\/p>\n<p>        nonlocal count<\/p>\n<p>        count += 1<\/p>\n<p>        return count<\/p>\n<p>    return counter<\/p>\n<p>counter = make_counter()<\/p>\n<p>print(counter())  # 1<\/p>\n<p>print(counter())  # 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_counter<\/code>\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305<code>counter<\/code>\uff0c\u5b83\u6355\u83b7\u4e86<code>count<\/code>\u7684\u503c\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u66f4\u65b0\u548c\u8fd4\u56de\u8fd9\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u95ed\u5305\u4e0e\u51fd\u6570\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><h4>1\u3001\u4ec0\u4e48\u662f\u51fd\u6570\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u95ed\u5305\uff0c\u5b83\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u7684\u884c\u4e3a\u3002\u88c5\u9970\u5668\u5728\u51fd\u6570\u5b9a\u4e49\u4e4b\u524d\u4f7f\u7528<code>@decorator_name<\/code>\u8bed\u6cd5\u8fdb\u884c\u6807\u6ce8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def say_hello():<\/p>\n<p>    print(&quot;Hello!&quot;)<\/p>\n<p>say_hello()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_decorator<\/code>\u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5b83\u4fee\u6539\u4e86<code>say_hello<\/code>\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u6253\u5370\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u88c5\u9970\u5668\u7684\u5de5\u4f5c\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fd4\u56de\u95ed\u5305\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff08\u95ed\u5305\uff09\uff0c\u8fd9\u4e2a\u95ed\u5305\u5305\u542b\u4e86\u5bf9\u539f\u51fd\u6570\u7684\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_decorator<\/code>\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305<code>wrapper<\/code>\uff0c\u5b83\u5728\u8c03\u7528\u539f\u51fd\u6570<code>func<\/code>\u524d\u540e\u6267\u884c\u989d\u5916\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u95ed\u5305\u4e0e\u7c7b\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><h4>1\u3001\u95ed\u5305\u4e0e\u7c7b\u7684\u76f8\u4f3c\u6027<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u548c\u7c7b\u5728\u67d0\u4e9b\u65b9\u9762\u5177\u6709\u76f8\u4f3c\u6027\uff0c\u5b83\u4eec\u90fd\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_counter():<\/p>\n<p>    count = 0<\/p>\n<p>    def counter():<\/p>\n<p>        nonlocal count<\/p>\n<p>        count += 1<\/p>\n<p>        return count<\/p>\n<p>    return counter<\/p>\n<p>class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>    def __call__(self):<\/p>\n<p>        self.count += 1<\/p>\n<p>        return self.count<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_counter<\/code>\u548c<code>Counter<\/code>\u7c7b\u90fd\u5b9e\u73b0\u4e86\u4e00\u4e2a\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u95ed\u5305\u4e0e\u7c7b\u7684\u533a\u522b<\/h4>\n<\/p>\n<p><p>\u5c3d\u7ba1\u95ed\u5305\u548c\u7c7b\u5728\u67d0\u4e9b\u65b9\u9762\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4e5f\u6709\u4e00\u4e9b\u91cd\u8981\u7684\u533a\u522b\u3002\u95ed\u5305\u901a\u5e38\u7528\u4e8e\u8f83\u7b80\u5355\u7684\u573a\u666f\uff0c\u800c\u7c7b\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">counter = make_counter()<\/p>\n<p>print(counter())  # 1<\/p>\n<p>print(counter())  # 2<\/p>\n<p>counter_obj = Counter()<\/p>\n<p>print(counter_obj())  # 1<\/p>\n<p>print(counter_obj())  # 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u95ed\u5305<code>counter<\/code>\u548c\u7c7b\u5b9e\u4f8b<code>counter_obj<\/code>\u90fd\u5b9e\u73b0\u4e86\u8ba1\u6570\u529f\u80fd\uff0c\u4f46\u7c7b\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u95ed\u5305\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><h4>1\u3001\u95ed\u5305\u7684\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5c01\u88c5\u6027<\/strong>\uff1a\u95ed\u5305\u53ef\u4ee5\u5c01\u88c5\u6570\u636e\u548c\u884c\u4e3a\uff0c\u907f\u514d\u6c61\u67d3\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3002<\/li>\n<li><strong>\u5ef6\u8fdf\u8ba1\u7b97<\/strong>\uff1a\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u5728\u9700\u8981\u65f6\u624d\u8fdb\u884c\u8ba1\u7b97\u3002<\/li>\n<li><strong>\u4fdd\u6301\u72b6\u6001<\/strong>\uff1a\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u4fdd\u6301\u51fd\u6570\u7684\u72b6\u6001\uff0c\u800c\u65e0\u9700\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u95ed\u5305\u7684\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u5185\u5b58\u5360\u7528<\/strong>\uff1a\u95ed\u5305\u4f1a\u6355\u83b7\u5916\u5c42\u51fd\u6570\u7684\u73af\u5883\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u589e\u52a0\u3002<\/li>\n<li><strong>\u8c03\u8bd5\u56f0\u96be<\/strong>\uff1a\u95ed\u5305\u7684\u8c03\u8bd5\u76f8\u5bf9\u56f0\u96be\uff0c\u56e0\u4e3a\u81ea\u7531\u53d8\u91cf\u7684\u503c\u53ef\u80fd\u5728\u4e0d\u540c\u7684\u6267\u884c\u73af\u5883\u4e2d\u53d8\u5316\u3002<\/li>\n<li><strong>\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u8fc7\u5ea6\u4f7f\u7528\u95ed\u5305\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u53ef\u8bfb\u6027\u4e0b\u964d\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u4e0d\u719f\u6089\u95ed\u5305\u6982\u5ff5\u7684\u5f00\u53d1\u8005\u3002<\/li>\n<\/ul>\n<p><h3>\u516b\u3001\u5b9e\u9645\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001\u6848\u4f8b\u4e00\uff1a\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cache(func):<\/p>\n<p>    cached_results = {}<\/p>\n<p>    def wrapper(*args):<\/p>\n<p>        if args in cached_results:<\/p>\n<p>            return cached_results[args]<\/p>\n<p>        result = func(*args)<\/p>\n<p>        cached_results[args] = result<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@cache<\/p>\n<p>def slow_function(x):<\/p>\n<p>    # \u6a21\u62df\u8017\u65f6\u8ba1\u7b97<\/p>\n<p>    import time<\/p>\n<p>    time.sleep(1)<\/p>\n<p>    return x * x<\/p>\n<p>print(slow_function(4))  # \u7b2c\u4e00\u6b21\u8c03\u7528\uff0c\u8ba1\u7b97\u5e76\u7f13\u5b58\u7ed3\u679c<\/p>\n<p>print(slow_function(4))  # \u7b2c\u4e8c\u6b21\u8c03\u7528\uff0c\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u88c5\u9970\u5668<code>cache<\/code>\u4f7f\u7528\u95ed\u5305\u7f13\u5b58\u8ba1\u7b97\u7ed3\u679c\uff0c\u63d0\u9ad8\u4e86\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6848\u4f8b\u4e8c\uff1a\u7f51\u9875\u8868\u5355\u9a8c\u8bc1<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u7f51\u9875\u8868\u5355\u9a8c\u8bc1\uff0c\u52a8\u6001\u751f\u6210\u9a8c\u8bc1\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_validator(min_length, max_length):<\/p>\n<p>    def validator(text):<\/p>\n<p>        if not (min_length &lt;= len(text) &lt;= max_length):<\/p>\n<p>            return False<\/p>\n<p>        return True<\/p>\n<p>    return validator<\/p>\n<p>name_validator = make_validator(3, 50)<\/p>\n<p>print(name_validator(&quot;Alice&quot;))  # True<\/p>\n<p>print(name_validator(&quot;A&quot;))      # False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_validator<\/code>\u8fd4\u56de\u4e00\u4e2a\u95ed\u5305<code>validator<\/code>\uff0c\u5b83\u6839\u636e\u6307\u5b9a\u7684\u6700\u5c0f\u548c\u6700\u5927\u957f\u5ea6\u9a8c\u8bc1\u8f93\u5165\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u95ed\u5305\u7684\u6df1\u5165\u7406\u89e3<\/h3>\n<\/p>\n<p><h4>1\u3001\u95ed\u5305\u4e0e\u5783\u573e\u56de\u6536<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u6355\u83b7\u7684\u81ea\u7531\u53d8\u91cf\u4f1a\u88ab\u4fdd\u5b58\u5728\u5185\u5c42\u51fd\u6570\u7684\u73af\u5883\u4e2d\uff0c\u76f4\u5230\u5185\u5c42\u51fd\u6570\u4e0d\u518d\u88ab\u5f15\u7528\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u95ed\u5305\u4e2d\u5305\u542b\u5927\u91cf\u6570\u636e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002Python\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u4f1a\u5904\u7406\u5927\u90e8\u5206\u60c5\u51b5\uff0c\u4f46\u5f00\u53d1\u8005\u4ecd\u9700\u6ce8\u610f\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u95ed\u5305\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_func():<\/p>\n<p>    large_data = [1] * 1000000<\/p>\n<p>    def inner_func():<\/p>\n<p>        return sum(large_data)<\/p>\n<p>    return inner_func<\/p>\n<p>closure = outer_func()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>large_data<\/code>\u4f1a\u88ab\u5185\u5c42\u51fd\u6570<code>inner_func<\/code>\u6355\u83b7\u5e76\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u76f4\u5230<code>closure<\/code>\u4e0d\u518d\u88ab\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u95ed\u5305\u4e0e\u533f\u540d\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u4e0e\u533f\u540d\u51fd\u6570\uff08lambda\u51fd\u6570\uff09\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u521b\u5efa\u7b80\u6d01\u7684\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_power(n):<\/p>\n<p>    return lambda x: x  n<\/p>\n<p>square = make_power(2)<\/p>\n<p>cube = make_power(3)<\/p>\n<p>print(square(4))  # 16<\/p>\n<p>print(cube(2))    # 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_power<\/code>\u8fd4\u56de\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6355\u83b7\u4e86\u81ea\u7531\u53d8\u91cf<code>n<\/code>\uff0c\u5e76\u8ba1\u7b97<code>x<\/code>\u7684<code>n<\/code>\u6b21\u5e42\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u95ed\u5305\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1\u3001\u9002\u5ea6\u4f7f\u7528\u95ed\u5305<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u95ed\u5305\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u4f46\u5e94\u9002\u5ea6\u4f7f\u7528\u3002\u8fc7\u5ea6\u4f7f\u7528\u95ed\u5305\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u590d\u6742\u6027\u589e\u52a0\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002\u5e94\u5728\u9002\u5f53\u7684\u573a\u666f\u4e0b\u4f7f\u7528\u95ed\u5305\uff0c\u4f8b\u5982\u5ef6\u8fdf\u8ba1\u7b97\u3001\u4fdd\u6301\u72b6\u6001\u548c\u51fd\u6570\u88c5\u9970\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6ce8\u610f\u95ed\u5305\u7684\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u589e\u52a0\uff0c\u5c24\u5176\u662f\u5728\u6355\u83b7\u5927\u91cf\u6570\u636e\u65f6\u3002\u5f00\u53d1\u8005\u5e94\u6ce8\u610f\u95ed\u5305\u7684\u6027\u80fd\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5360\u7528\u3002\u5728\u9700\u8981\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u95ed\u5305\u7684\u573a\u666f\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7c7b\u6765\u66ff\u4ee3\u95ed\u5305\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4fdd\u6301\u4ee3\u7801\u53ef\u8bfb\u6027<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u7684\u4f7f\u7528\u5e94\u5c3d\u91cf\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u907f\u514d\u8fc7\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\u3002\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u7684\u547d\u540d\u548c\u6ce8\u91ca\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f7f\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u66f4\u5bb9\u6613\u5730\u7406\u89e3\u548c\u7ef4\u62a4\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u95ed\u5305\u51fd\u6570\u901a\u8fc7\u51fd\u6570\u5d4c\u5957\u3001\u81ea\u7531\u53d8\u91cf\u548c\u8fd4\u56de\u5185\u5c42\u51fd\u6570\u5b9e\u73b0\u5bf9\u5916\u5c42\u51fd\u6570\u5c40\u90e8\u53d8\u91cf\u7684\u5f15\u7528\u3002\u95ed\u5305\u5728\u5ef6\u8fdf\u8ba1\u7b97\u3001\u4fdd\u6301\u72b6\u6001\u548c\u51fd\u6570\u88c5\u9970\u5668\u7b49\u573a\u666f\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002\u5f00\u53d1\u8005\u5e94\u9002\u5ea6\u4f7f\u7528\u95ed\u5305\uff0c\u6ce8\u610f\u95ed\u5305\u7684\u6027\u80fd\u548c\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u4ee5\u5b9e\u73b0\u66f4\u52a0\u9ad8\u6548\u548c\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u6df1\u5165\u7406\u89e3\u95ed\u5305\u7684\u6267\u884c\u673a\u5236\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u8fd9\u4e00\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u63d0\u5347Python\u7f16\u7a0b\u7684\u80fd\u529b\u548c\u6c34\u5e73\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u4e2d\u7684\u95ed\u5305\u51fd\u6570\uff1f<\/strong><br \/>\u95ed\u5305\u51fd\u6570\u662f\u4e00\u4e2a\u5d4c\u5957\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u8bb0\u4f4f\u5e76\u8bbf\u95ee\u5176\u5916\u90e8\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u8fd4\u56de\u3002\u95ed\u5305\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u5b83\u80fd\u591f\u5c06\u4e00\u4e9b\u6570\u636e\u7ed1\u5b9a\u5230\u4e00\u4e2a\u51fd\u6570\u4e0a\uff0c\u4ece\u800c\u5f62\u6210\u4e00\u4e2a\u53ef\u8c03\u7528\u7684\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u95ed\u5305\u51fd\u6570\u6709\u4ec0\u4e48\u5b9e\u9645\u5e94\u7528\uff1f<\/strong><br \/>\u95ed\u5305\u51fd\u6570\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u6709\u591a\u79cd\u5e94\u7528\u3002\u5b83\u4eec\u5e38\u7528\u4e8e\u521b\u5efa\u5de5\u5382\u51fd\u6570\u3001\u5b9e\u73b0\u6570\u636e\u5c01\u88c5\u548c\u79c1\u6709\u53d8\u91cf\uff0c\u4ee5\u53ca\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u4fdd\u7559\u72b6\u6001\u3002\u901a\u8fc7\u95ed\u5305\uff0c\u53ef\u4ee5\u5728\u4e0d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u7684\u60c5\u51b5\u4e0b\u5b9e\u73b0\u72b6\u6001\u7684\u6301\u4e45\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u95ed\u5305\u51fd\u6570\uff1f<\/strong><br \/>\u521b\u5efa\u95ed\u5305\u51fd\u6570\u7684\u6b65\u9aa4\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u3002\u5916\u90e8\u51fd\u6570\u9700\u8981\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\u3002\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u81ea\u7531\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u4ece\u800c\u5f62\u6210\u95ed\u5305\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def outer_function(x):\n    def inner_function(y):\n        return x + y\n    return inner_function\n\nclosure = outer_function(10)\nresult = closure(5)  # result \u4e3a 15\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code>\u5c31\u662f\u4e00\u4e2a\u95ed\u5305\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee<code>outer_function<\/code>\u7684\u53c2\u6570<code>x<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u95ed\u5305\u51fd\u6570\u6267\u884c\u7684\u6838\u5fc3\u5728\u4e8e\uff1a\u51fd\u6570\u5d4c\u5957\u3001\u81ea\u7531\u53d8\u91cf\u3001\u8fd4\u56de\u5185\u5c42\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u81ea\u7531\u53d8\u91cf\u7684\u6982\u5ff5\u5c24\u4e3a\u91cd\u8981\uff0c\u81ea\u7531\u53d8\u91cf\u662f [&hellip;]","protected":false},"author":3,"featured_media":1159998,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159988"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1159988"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159988\/revisions"}],"predecessor-version":[{"id":1160002,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159988\/revisions\/1160002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159998"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}