{"id":1174569,"date":"2025-01-15T17:19:58","date_gmt":"2025-01-15T09:19:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174569.html"},"modified":"2025-01-15T17:20:00","modified_gmt":"2025-01-15T09:20:00","slug":"python%e9%97%ad%e5%8c%85%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1174569.html","title":{"rendered":"python\u95ed\u5305\u51fd\u6570\u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26080025\/2e0d1c7c-e346-4b93-9829-47372771e7db.webp\" alt=\"python\u95ed\u5305\u51fd\u6570\u5982\u4f55\u8c03\u7528\" \/><\/p>\n<p><p> <strong>Python\u95ed\u5305\u51fd\u6570\u7684\u8c03\u7528\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u3001\u5728\u5916\u90e8\u51fd\u6570\u5185\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u3001\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\u3001\u8c03\u7528\u8fd4\u56de\u7684\u5185\u90e8\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u6570\u636e\u7684\u5c01\u88c5\u548c\u6301\u4e45\u5316<\/strong>\uff0c\u5728\u4e00\u4e9b\u7279\u5b9a\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u95ed\u5305\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u80fd\u591f\u6355\u83b7\u548c\u8bb0\u4f4f\u5176\u6240\u5728\u7684\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5728\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ecd\u7136\u5b58\u5728\u3002\u95ed\u5305\u7684\u4e00\u4e2a\u5178\u578b\u5e94\u7528\u573a\u666f\u662f\u5b9e\u73b0\u5de5\u5382\u51fd\u6570\u6216\u88c5\u9970\u5668\u3002\u5728\u5b9a\u4e49\u548c\u8c03\u7528\u95ed\u5305\u51fd\u6570\u65f6\uff0c\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u5e76\u4e14\u5185\u90e8\u51fd\u6570\u5f15\u7528\u4e86\u5916\u90e8\u51fd\u6570\u4e2d\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u7136\u540e\uff0c\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u4ece\u800c\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u95ed\u5305\u51fd\u6570\u7684\u5b9a\u4e49\u3001\u4f7f\u7528\u573a\u666f\u548c\u8c03\u7528\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u95ed\u5305\u51fd\u6570\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u662f\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u8fd9\u4f7f\u5f97\u95ed\u5305\u51fd\u6570\u6210\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5de5\u5382\u51fd\u6570\u3001\u88c5\u9970\u5668\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5916\u90e8\u51fd\u6570\u548c\u5185\u90e8\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u901a\u5e38\u7531\u4e24\u4e2a\u51fd\u6570\u7ec4\u6210\uff1a\u5916\u90e8\u51fd\u6570\u548c\u5185\u90e8\u51fd\u6570\u3002\u5916\u90e8\u51fd\u6570\u5b9a\u4e49\u4e86\u5185\u90e8\u51fd\u6570\u5e76\u8fd4\u56de\u5b83\uff0c\u5185\u90e8\u51fd\u6570\u5219\u4f7f\u7528\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function():<\/p>\n<p>    x = 10<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(x)<\/p>\n<p>    return inner_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code> \u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u5f15\u7528\u4e86 <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u8fd4\u56de\u5185\u90e8\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u5916\u90e8\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">closure = outer_function()<\/p>\n<p>closure()  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u95ed\u5305\u51fd\u6570\u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u5728\u8bb8\u591a\u7f16\u7a0b\u573a\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u5c01\u88c5<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5c01\u88c5\u6570\u636e\uff0c\u4f7f\u5176\u4e0d\u80fd\u88ab\u5916\u90e8\u76f4\u63a5\u8bbf\u95ee\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())  # \u8f93\u51fa\uff1a1<\/p>\n<p>print(counter())  # \u8f93\u51fa\uff1a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>count<\/code> \u662f\u4e00\u4e2a\u5c01\u88c5\u5728 <code>make_counter<\/code> \u4e2d\u7684\u53d8\u91cf\uff0c\u53ea\u6709\u901a\u8fc7 <code>counter<\/code> \u51fd\u6570\u624d\u80fd\u8bbf\u95ee\u548c\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h4>2. \u5de5\u5382\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5177\u6709\u7279\u5b9a\u884c\u4e3a\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u5171\u4eab\u4e00\u4e9b\u5171\u540c\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def power_factory(exponent):<\/p>\n<p>    def power(base):<\/p>\n<p>        return base  exponent<\/p>\n<p>    return power<\/p>\n<p>square = power_factory(2)<\/p>\n<p>cube = power_factory(3)<\/p>\n<p>print(square(4))  # \u8f93\u51fa\uff1a16<\/p>\n<p>print(cube(2))    # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>power_factory<\/code> \u521b\u5efa\u4e86\u4e24\u4e2a\u95ed\u5305\u51fd\u6570 <code>square<\/code> \u548c <code>cube<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u8ba1\u7b97\u5e73\u65b9\u548c\u7acb\u65b9\u3002<\/p>\n<\/p>\n<p><h4>3. \u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u95ed\u5305\u51fd\u6570\u7684\u4e00\u4e2a\u7ecf\u5178\u5e94\u7528\uff0c\u5b83\u4eec\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u6216\u6269\u5c55\u5176\u4ed6\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Before calling function&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;After calling function&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@decorator<\/p>\n<p>def say_hello(name):<\/p>\n<p>    print(f&quot;Hello, {name}&quot;)<\/p>\n<p>say_hello(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>decorator<\/code> \u662f\u4e00\u4e2a\u95ed\u5305\u51fd\u6570\uff0c\u5b83\u5728\u8c03\u7528 <code>say_hello<\/code> \u51fd\u6570\u524d\u540e\u6253\u5370\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u95ed\u5305\u51fd\u6570\u7684\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u7684\u8c03\u7528\u8fc7\u7a0b\u5305\u62ec\u5b9a\u4e49\u5916\u90e8\u51fd\u6570\u3001\u5185\u90e8\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5b9a\u4e49\u5916\u90e8\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5305\u542b\u9700\u8981\u5c01\u88c5\u7684\u53d8\u91cf\u548c\u5185\u90e8\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(x)<\/p>\n<p>    return inner_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u5185\u90e8\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5916\u90e8\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(x)<\/p>\n<p>    return inner_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528<\/h4>\n<\/p>\n<p><p>\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u5916\u90e8\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">closure = outer_function(10)<\/p>\n<p>closure()  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8c03\u7528\u95ed\u5305\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u7528\u5916\u90e8\u51fd\u6570\u5e76\u83b7\u53d6\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u7136\u540e\u8c03\u7528\u5185\u90e8\u51fd\u6570\u6765\u5b9e\u73b0\u95ed\u5305\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">closure = outer_function(10)<\/p>\n<p>closure()  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u95ed\u5305\u51fd\u6570\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u7528\u6cd5\uff0c\u95ed\u5305\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u7f16\u7a0b\u573a\u666f\uff0c\u5982\u72b6\u6001\u7ba1\u7406\u3001\u56de\u8c03\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u72b6\u6001\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u7ba1\u7406\u590d\u6742\u7684\u72b6\u6001\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def state_manager(initial_state):<\/p>\n<p>    state = initial_state<\/p>\n<p>    def get_state():<\/p>\n<p>        return state<\/p>\n<p>    def set_state(new_state):<\/p>\n<p>        nonlocal state<\/p>\n<p>        state = new_state<\/p>\n<p>    return get_state, set_state<\/p>\n<p>get_state, set_state = state_manager(0)<\/p>\n<p>print(get_state())  # \u8f93\u51fa\uff1a0<\/p>\n<p>set_state(42)<\/p>\n<p>print(get_state())  # \u8f93\u51fa\uff1a42<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>state_manager<\/code> \u4f7f\u7528\u95ed\u5305\u51fd\u6570\u6765\u7ba1\u7406\u72b6\u6001 <code>state<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u56de\u8c03\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u4f5c\u56de\u8c03\u51fd\u6570\uff0c\u5c24\u5176\u662f\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_callback(message):<\/p>\n<p>    def callback():<\/p>\n<p>        print(message)<\/p>\n<p>    return callback<\/p>\n<p>callback = create_callback(&quot;Hello, World!&quot;)<\/p>\n<p>callback()  # \u8f93\u51fa\uff1aHello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>create_callback<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5e26\u6709\u7279\u5b9a\u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>3. \u5ef6\u8fdf\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u6267\u884c\u8ba1\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def lazy_computation(x, y):<\/p>\n<p>    def compute():<\/p>\n<p>        return x + y<\/p>\n<p>    return compute<\/p>\n<p>computation = lazy_computation(5, 10)<\/p>\n<p>print(computation())  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>lazy_computation<\/code> \u4f7f\u7528\u95ed\u5305\u51fd\u6570\u6765\u5ef6\u8fdf\u8ba1\u7b97 <code>x + y<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u95ed\u5305\u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u65f6\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u7262\u8bb0\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1. \u53d8\u91cf\u7684\u4f5c\u7528\u57df<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ea\u80fd\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u800c\u4e0d\u80fd\u4fee\u6539\u5b83\u4eec\uff0c\u9664\u975e\u4f7f\u7528 <code>nonlocal<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def inner_function():<\/p>\n<p>        nonlocal x<\/p>\n<p>        x += 1<\/p>\n<p>        print(x)<\/p>\n<p>    return inner_function<\/p>\n<p>closure = outer_function(10)<\/p>\n<p>closure()  # \u8f93\u51fa\uff1a11<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5185\u5b58\u6cc4\u6f0f<\/h4>\n<\/p>\n<p><p>\u4e0d\u6b63\u786e\u5730\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u4e3a\u95ed\u5305\u4f1a\u4fdd\u7559\u5bf9\u5916\u90e8\u51fd\u6570\u53d8\u91cf\u7684\u5f15\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_large_closure():<\/p>\n<p>    large_data = [0] * 1000000<\/p>\n<p>    def closure():<\/p>\n<p>        print(len(large_data))<\/p>\n<p>    return closure<\/p>\n<p>closure = create_large_closure()<\/p>\n<p>closure()  # \u8f93\u51fa\uff1a1000000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>large_data<\/code> \u4f1a\u4e00\u76f4\u4fdd\u7559\u5728\u5185\u5b58\u4e2d\uff0c\u76f4\u5230\u95ed\u5305\u51fd\u6570\u88ab\u9500\u6bc1\u3002<\/p>\n<\/p>\n<p><h4>3. \u8c03\u8bd5\u56f0\u96be<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u95ed\u5305\u51fd\u6570\u6355\u83b7\u4e86\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u8c03\u8bd5\u95ed\u5305\u51fd\u6570\u53ef\u80fd\u6bd4\u666e\u901a\u51fd\u6570\u66f4\u52a0\u56f0\u96be\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u95ed\u5305\u51fd\u6570\u7684\u66ff\u4ee3\u65b9\u6848<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u95ed\u5305\u51fd\u6570\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u548c\u5bf9\u8c61\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u7ba1\u7406\u590d\u6742\u72b6\u6001\u65f6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u7c7b\u548c\u5bf9\u8c61\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u66f4\u597d\u7684\u4ee3\u7801\u7ec4\u7ec7\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66ff\u4ee3\u95ed\u5305\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>    def increment(self):<\/p>\n<p>        self.count += 1<\/p>\n<p>        return self.count<\/p>\n<p>counter = Counter()<\/p>\n<p>print(counter.increment())  # \u8f93\u51fa\uff1a1<\/p>\n<p>print(counter.increment())  # \u8f93\u51fa\uff1a2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Counter<\/code> \u7c7b\u5b9e\u73b0\u4e86\u4e0e\u95ed\u5305\u51fd\u6570 <code>make_counter<\/code> \u76f8\u540c\u7684\u529f\u80fd\uff0c\u4f46\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u51fd\u6570\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u5bf9\u8c61\uff08functools.partial\uff09\u53ef\u4ee5\u66ff\u4ee3\u95ed\u5305\u51fd\u6570\u6765\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import partial<\/p>\n<p>def power(base, exponent):<\/p>\n<p>    return base  exponent<\/p>\n<p>square = partial(power, exponent=2)<\/p>\n<p>cube = partial(power, exponent=3)<\/p>\n<p>print(square(4))  # \u8f93\u51fa\uff1a16<\/p>\n<p>print(cube(2))    # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>functools.partial<\/code> \u521b\u5efa\u4e86\u90e8\u5206\u5e94\u7528\u51fd\u6570 <code>square<\/code> \u548c <code>cube<\/code>\uff0c\u5b83\u4eec\u5b9e\u73b0\u4e86\u4e0e\u95ed\u5305\u51fd\u6570 <code>power_factory<\/code> \u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u95ed\u5305\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u95ed\u5305\u51fd\u6570\u5728\u8bb8\u591a\u573a\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u6027\u80fd\u5f00\u9500\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u95ed\u5305\u51fd\u6570\u6027\u80fd\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u907f\u514d\u4e0d\u5fc5\u8981\u7684\u95ed\u5305<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u95ed\u5305\u51fd\u6570\u8f6c\u6362\u4e3a\u666e\u901a\u51fd\u6570\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compute(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>result = compute(5, 10)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u76f4\u63a5\u8c03\u7528 <code>compute<\/code> \u51fd\u6570\u53ef\u4ee5\u907f\u514d\u95ed\u5305\u51fd\u6570\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><h4>2. \u51cf\u5c11\u95ed\u5305\u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6b21<\/h4>\n<\/p>\n<p><p>\u51cf\u5c11\u95ed\u5305\u51fd\u6570\u7684\u5d4c\u5957\u5c42\u6b21\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def middle_function(y):<\/p>\n<p>        def inner_function(z):<\/p>\n<p>            return x + y + z<\/p>\n<p>        return inner_function<\/p>\n<p>    return middle_function<\/p>\n<p>middle = outer_function(5)<\/p>\n<p>inner = middle(10)<\/p>\n<p>print(inner(15))  # \u8f93\u51fa\uff1a30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51cf\u5c11\u5d4c\u5957\u5c42\u6b21\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u7f13\u5b58\u6765\u4f18\u5316\u95ed\u5305\u51fd\u6570\u7684\u6027\u80fd\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def memoize(f):<\/p>\n<p>    cache = {}<\/p>\n<p>    def memoized_function(*args):<\/p>\n<p>        if args not in cache:<\/p>\n<p>            cache[args] = f(*args)<\/p>\n<p>        return cache[args]<\/p>\n<p>    return memoized_function<\/p>\n<p>@memoize<\/p>\n<p>def compute(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>print(compute(5, 10))  # \u8f93\u51fa\uff1a15<\/p>\n<p>print(compute(5, 10))  # \u8f93\u51fa\uff1a15\uff08\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\uff09<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>memoize<\/code> \u88c5\u9970\u5668\u4f7f\u7528\u7f13\u5b58\u6765\u4f18\u5316 <code>compute<\/code> \u51fd\u6570\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u95ed\u5305\u51fd\u6570\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u4f7f\u7528\u95ed\u5305\u51fd\u6570\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fdd\u6301\u95ed\u5305\u51fd\u6570\u7b80\u6d01<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u5e94\u4fdd\u6301\u7b80\u6d01\uff0c\u907f\u514d\u5305\u542b\u8fc7\u591a\u7684\u903b\u8f91\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\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())  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>counter<\/code> \u95ed\u5305\u51fd\u6570\u4fdd\u6301\u7b80\u6d01\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6ce8\u91ca\u548c\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u65f6\uff0c\u5e94\u6dfb\u52a0\u6ce8\u91ca\u548c\u6587\u6863\uff0c\u4ee5\u4fbf\u5176\u4ed6\u5f00\u53d1\u8005\u80fd\u591f\u7406\u89e3\u4ee3\u7801\u7684\u610f\u56fe\u548c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def power_factory(exponent):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e00\u4e2a\u8ba1\u7b97\u7ed9\u5b9a\u5e95\u6570\u7684\u6307\u5b9a\u6307\u6570\u7684\u51fd\u6570\u3002<\/p>\n<p>    \u53c2\u6570\uff1a<\/p>\n<p>    exponent (int): \u6307\u6570<\/p>\n<p>    \u8fd4\u56de\uff1a<\/p>\n<p>    function: \u8ba1\u7b97\u7ed9\u5b9a\u5e95\u6570\u7684\u51fd\u6570<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def power(base):<\/p>\n<p>        return base  exponent<\/p>\n<p>    return power<\/p>\n<p>square = power_factory(2)<\/p>\n<p>print(square(4))  # \u8f93\u51fa\uff1a16<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6dfb\u52a0\u4e86\u6ce8\u91ca\u548c\u6587\u6863\uff0c\u5e2e\u52a9\u5176\u4ed6\u5f00\u53d1\u8005\u7406\u89e3 <code>power_factory<\/code> \u51fd\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3. \u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u95ed\u5305<\/h4>\n<\/p>\n<p><p>\u867d\u7136\u95ed\u5305\u51fd\u6570\u975e\u5e38\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7c7b\u548c\u5bf9\u8c61\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u95ed\u5305\uff0c\u4ee5\u4fdd\u6301\u4ee3\u7801\u7684\u6e05\u6670\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Counter:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.count = 0<\/p>\n<p>    def increment(self):<\/p>\n<p>        self.count += 1<\/p>\n<p>        return self.count<\/p>\n<p>counter = Counter()<\/p>\n<p>print(counter.increment())  # \u8f93\u51fa\uff1a1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528\u7c7b\u6765\u5b9e\u73b0\u8ba1\u6570\u5668\u529f\u80fd\uff0c\u6bd4\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u95ed\u5305\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u8bb8\u591a\u7528\u4f8b\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u4e8b\u4ef6\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u5728\u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u4e8b\u4ef6\u5e76\u4fdd\u7559\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_event_handler(event_type):<\/p>\n<p>    def handle_event(event):<\/p>\n<p>        if event.type == event_type:<\/p>\n<p>            print(f&quot;Handling {event_type} event&quot;)<\/p>\n<p>    return handle_event<\/p>\n<p>click_handler = create_event_handler(&quot;click&quot;)<\/p>\n<p>click_handler(Event(&quot;click&quot;))  # \u8f93\u51fa\uff1aHandling click event<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>create_event_handler<\/code> \u521b\u5efa\u4e86\u4e00\u4e2a\u5904\u7406\u7279\u5b9a\u4e8b\u4ef6\u7c7b\u578b\u7684\u95ed\u5305\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u52a8\u6001\u751f\u6210\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u52a8\u6001\u751f\u6210\u5177\u6709\u7279\u5b9a\u884c\u4e3a\u7684\u51fd\u6570\uff0c\u907f\u514d\u91cd\u590d\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_multiplier(factor):<\/p>\n<p>    def multiplier(x):<\/p>\n<p>        return x * factor<\/p>\n<p>    return multiplier<\/p>\n<p>double = make_multiplier(2)<\/p>\n<p>triple = make_multiplier(3)<\/p>\n<p>print(double(5))  # \u8f93\u51fa\uff1a10<\/p>\n<p>print(triple(5))  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>make_multiplier<\/code> \u52a8\u6001\u751f\u6210\u4e86\u4e24\u4e2a\u51fd\u6570 <code>double<\/code> \u548c <code>triple<\/code>\uff0c\u5b83\u4eec\u5206\u522b\u5c06\u8f93\u5165\u503c\u4e58\u4ee5 2 \u548c 3\u3002<\/p>\n<\/p>\n<p><h4>3. \u8bbf\u95ee\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u5bf9\u67d0\u4e9b\u53d8\u91cf\u7684\u8bbf\u95ee\uff0c\u63d0\u4f9b\u4e00\u79cd\u7b80\u6d01\u7684\u5c01\u88c5\u673a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_account(initial_balance):<\/p>\n<p>    balance = initial_balance<\/p>\n<p>    def get_balance():<\/p>\n<p>        return balance<\/p>\n<p>    def deposit(amount):<\/p>\n<p>        nonlocal balance<\/p>\n<p>        balance += amount<\/p>\n<p>    def withdraw(amount):<\/p>\n<p>        nonlocal balance<\/p>\n<p>        if amount &lt;= balance:<\/p>\n<p>            balance -= amount<\/p>\n<p>            return True<\/p>\n<p>        return False<\/p>\n<p>    return get_balance, deposit, withdraw<\/p>\n<p>get_balance, deposit, withdraw = create_account(100)<\/p>\n<p>print(get_balance())  # \u8f93\u51fa\uff1a100<\/p>\n<p>deposit(50)<\/p>\n<p>print(get_balance())  # \u8f93\u51fa\uff1a150<\/p>\n<p>print(withdraw(30))   # \u8f93\u51fa\uff1aTrue<\/p>\n<p>print(get_balance())  # \u8f93\u51fa\uff1a120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>create_account<\/code> \u4f7f\u7528\u95ed\u5305\u51fd\u6570\u6765\u63a7\u5236\u5bf9\u8d26\u6237\u4f59\u989d\u7684\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u662fPython\u4e2d\u7684\u4e00\u79cd\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u6355\u83b7\u5e76\u8bb0\u4f4f\u5176\u6240\u5728\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5728\u51fd\u6570\u8c03\u7528\u7ed3\u675f\u540e\uff0c\u8fd9\u4e9b\u53d8\u91cf\u4ecd\u7136\u5b58\u5728\u3002\u95ed\u5305\u51fd\u6570\u7684\u5b9a\u4e49\u5305\u62ec\u5916\u90e8\u51fd\u6570\u548c\u5185\u90e8\u51fd\u6570\uff0c\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u4ece\u800c\u53ef\u4ee5\u5728\u5916\u90e8\u8c03\u7528\u5185\u90e8\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u95ed\u5305\u51fd\u6570\u5728\u6570\u636e\u5c01\u88c5\u3001\u5de5\u5382\u51fd\u6570\u3001\u88c5\u9970\u5668\u3001\u72b6\u6001\u7ba1\u7406\u3001\u56de\u8c03\u51fd\u6570\u3001\u5ef6\u8fdf\u8ba1\u7b97\u7b49\u65b9\u9762\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3001\u5185\u5b58\u6cc4\u6f0f\u548c\u8c03\u8bd5\u56f0\u96be\u7b49\u95ee\u9898\u3002\u7c7b\u548c\u5bf9\u8c61\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u66ff\u4ee3\u95ed\u5305\u51fd\u6570\uff0c\u66f4\u597d\u5730\u7ba1\u7406\u590d\u6742\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9075\u5faa\u6700\u4f73\u5b9e\u8df5\uff0c\u5982\u4fdd\u6301\u95ed\u5305\u51fd\u6570\u7b80\u6d01\u3001\u4f7f\u7528\u6ce8\u91ca\u548c\u6587\u6863\u3001\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u95ed\u5305\u7b49\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u4f7f\u7528\u95ed\u5305\u51fd\u6570\u3002\u95ed\u5305\u51fd\u6570\u5728\u4e8b\u4ef6\u5904\u7406\u3001\u52a8\u6001\u751f\u6210\u51fd\u6570\u3001\u8bbf\u95ee\u63a7\u5236\u7b49\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u7740\u91cd\u8981\u4f5c\u7528\u3002\u4f18\u5316\u95ed\u5305\u51fd\u6570\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\u548c\u53ef\u7ef4\u62a4\u6027\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\u6307\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u95ed\u5305\u5141\u8bb8\u60a8\u5c06\u4e00\u4e9b\u51fd\u6570\u7684\u72b6\u6001\u6301\u4e45\u5316\uff0c\u80fd\u591f\u5728\u5916\u90e8\u73af\u5883\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u72b6\u6001\u800c\u4e0d\u9700\u8981\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<p><strong>\u95ed\u5305\u51fd\u6570\u4e0e\u666e\u901a\u51fd\u6570\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>\u95ed\u5305\u51fd\u6570\u4e0e\u666e\u901a\u51fd\u6570\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u5176\u4f5c\u7528\u57df\u3002\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5176\u5916\u90e8\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u666e\u901a\u51fd\u6570\u53ea\u80fd\u8bbf\u95ee\u5168\u5c40\u53d8\u91cf\u6216\u5176\u81ea\u8eab\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u8fd9\u79cd\u7279\u6027\u4f7f\u5f97\u95ed\u5305\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5c01\u88c5\u72b6\u6001\u548c\u884c\u4e3a\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u6570\u636e\u9690\u85cf\u548c\u7ec4\u7ec7\u7ed3\u6784\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u8c03\u7528\u95ed\u5305\u51fd\u6570\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u548c\u8c03\u7528\u95ed\u5305\u51fd\u6570\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\uff0c\u7136\u540e\u5728\u5176\u5185\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u8fd9\u4e2a\u5185\u90e8\u51fd\u6570\u3002\u8c03\u7528\u5916\u90e8\u51fd\u6570\u65f6\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u5230\u5185\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5e76\u901a\u8fc7\u8fd9\u4e2a\u5f15\u7528\u6765\u8c03\u7528\u95ed\u5305\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def outer_function(msg):\n    def inner_function():\n        print(msg)\n    return inner_function\n\nclosure = outer_function(&quot;Hello, Closure!&quot;)\nclosure()  # \u8f93\u51fa: Hello, Closure!\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>inner_function<\/code>\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee<code>outer_function<\/code>\u4e2d\u7684<code>msg<\/code>\u53d8\u91cf\uff0c\u5c3d\u7ba1<code>outer_function<\/code>\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u95ed\u5305\u51fd\u6570\u7684\u8c03\u7528\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b9a\u4e49\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u3001\u5728\u5916\u90e8\u51fd\u6570\u5185\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u51fd\u6570\u3001\u5916\u90e8\u51fd\u6570\u8fd4\u56de\u5185\u90e8\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1174576,"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\/1174569"}],"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=1174569"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174569\/revisions"}],"predecessor-version":[{"id":1174577,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174569\/revisions\/1174577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1174576"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}