{"id":1182012,"date":"2025-01-15T18:58:23","date_gmt":"2025-01-15T10:58:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182012.html"},"modified":"2025-01-15T18:58:26","modified_gmt":"2025-01-15T10:58:26","slug":"python%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%92%8c%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1182012.html","title":{"rendered":"python\u51fd\u6570\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25130141\/6a8941ae-00aa-4626-938e-9fc4d482c47e.webp\" alt=\"python\u51fd\u6570\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Python\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u8fdb\u884c\uff1a\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528def\u5173\u952e\u5b57\u3001\u51fd\u6570\u540d\u5e94\u6709\u610f\u4e49\u3001\u5305\u542b\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3001\u5728\u51fd\u6570\u4f53\u5185\u7f16\u5199\u4ee3\u7801\u3001\u8c03\u7528\u51fd\u6570\u3002<\/strong> \u5176\u4e2d\uff0c\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u8c03\u7528\u662f\u6838\u5fc3\u90e8\u5206\uff0c\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528Python\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u51fd\u6570\u662f\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u7684\u3002\u51fd\u6570\u7684\u5b9a\u4e49\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u3001\u51fd\u6570\u4f53\u548c\u53ef\u9009\u7684\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>name<\/code>\uff0c\u5e76\u6253\u5370\u4e00\u4e2a\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u548c\u53c2\u6570\u6765\u8c03\u7528\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u51fd\u6570\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002\u53c2\u6570\u5206\u4e3a\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u4f4d\u7f6e\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u6700\u5e38\u89c1\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u5b83\u4eec\u6309\u7167\u4f4d\u7f6e\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>result = add(3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5173\u952e\u5b57\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u662f\u5728\u8c03\u7528\u51fd\u6570\u65f6\u660e\u786e\u6307\u5b9a\u53c2\u6570\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def introduce(name, age):<\/p>\n<p>    print(f&quot;My name is {name} and I am {age} years old.&quot;)<\/p>\n<p>introduce(name=&quot;Alice&quot;, age=30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>My name is Alice and I am 30 years old.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u9ed8\u8ba4\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u6709\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u8c03\u7528\u65f6\u672a\u63d0\u4f9b\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name=&quot;Guest&quot;):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>greet()  # \u8f93\u51fa Hello, Guest!<\/p>\n<p>greet(&quot;Alice&quot;)  # \u8f93\u51fa Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u53ef\u53d8\u53c2\u6570<\/h3>\n<\/p>\n<p><p>Python\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u6765\u5904\u7406\u53ef\u53d8\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>*args<\/h4>\n<\/p>\n<p><p><code>*args<\/code>\u7528\u4e8e\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_numbers(*args):<\/p>\n<p>    return sum(args)<\/p>\n<p>result = sum_numbers(1, 2, 3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>kwargs<\/h4>\n<\/p>\n<p><p><code>kwargs<\/code>\u7528\u4e8e\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u5178\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_info(kwargs):<\/p>\n<p>    for key, value in kwargs.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>print_info(name=&quot;Alice&quot;, age=30, city=&quot;New York&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>name: Alice<\/p>\n<p>age: 30<\/p>\n<p>city: New York<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u5982\u679c\u6ca1\u6709<code>return<\/code>\u8bed\u53e5\uff0c\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de<code>None<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<p>result = multiply(3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa 12<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u7528\u4e8e\u4e3a\u51fd\u6570\u6dfb\u52a0\u8bf4\u660e\u6587\u6863\u3002\u5b83\u4eec\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u8868\u793a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This function greets the person with the given name.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>print(greet.__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>This function greets the person with the given name.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5d4c\u5957\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5141\u8bb8\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u79f0\u4e3a\u5d4c\u5957\u51fd\u6570\u6216\u5185\u90e8\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(text):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(text)<\/p>\n<p>    inner_function()<\/p>\n<p>outer_function(&quot;Hello from inner function!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello from inner function!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u9ad8\u9636\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u6216\u8005\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u7ed3\u679c\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def apply_function(func, value):<\/p>\n<p>    return func(value)<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>result = apply_function(square, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd4\u56de\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def create_multiplier(n):<\/p>\n<p>    def multiplier(x):<\/p>\n<p>        return x * n<\/p>\n<p>    return multiplier<\/p>\n<p>double = create_multiplier(2)<\/p>\n<p>result = double(5)<\/p>\n<p>print(result)  # \u8f93\u51fa 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u533f\u540d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u662f\u4f7f\u7528<code>lambda<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u7684\u77ed\u5c0f\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">square = lambda x: x * x<\/p>\n<p>print(square(5))  # \u8f93\u51fa 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u76f4\u63a5\u6216\u95f4\u63a5\u8c03\u7528\u81ea\u8eab\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u9636\u4e58\u7684\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 1:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)<\/p>\n<p>print(factorial(5))  # \u8f93\u51fa 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5373\u4f7f\u5728\u5916\u90e8\u51fd\u6570\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(msg):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(msg)<\/p>\n<p>    return inner_function<\/p>\n<p>closure = outer_function(&quot;Hello, World!&quot;)<\/p>\n<p>closure()  # \u8f93\u51fa Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4fee\u6539\u6216\u589e\u5f3a\u51fd\u6570\u884c\u4e3a\u7684\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u901a\u5e38\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u8bbf\u95ee\u63a7\u5236\u548c\u6027\u80fd\u6d4b\u91cf\u7b49\u573a\u666f\u3002\u88c5\u9970\u5668\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u7684\u9ad8\u9636\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def decorator_function(original_function):<\/p>\n<p>    def wrapper_function():<\/p>\n<p>        print(f&quot;Wrapper executed this before {original_function.__name__}&quot;)<\/p>\n<p>        return original_function()<\/p>\n<p>    return wrapper_function<\/p>\n<p>@decorator_function<\/p>\n<p>def display():<\/p>\n<p>    print(&quot;Display function ran&quot;)<\/p>\n<p>display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Wrapper executed this before display<\/p>\n<p>Display function ran<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662f\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u53ef\u4ee5\u9010\u4e00\u751f\u6210\u503c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def countdown(n):<\/p>\n<p>    while n &gt; 0:<\/p>\n<p>        yield n<\/p>\n<p>        n -= 1<\/p>\n<p>for number in countdown(5):<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>5<\/p>\n<p>4<\/p>\n<p>3<\/p>\n<p>2<\/p>\n<p>1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u5f02\u6b65\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u51fd\u6570\u4f7f\u7528<code>async<\/code>\u548c<code>aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u7528\u4e8e\u5f02\u6b65\u7f16\u7a0b\uff0c\u5141\u8bb8\u51fd\u6570\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u6682\u505c\u5e76\u91ca\u653e\u63a7\u5236\u6743\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4ee3\u7801\u53ef\u4ee5\u6267\u884c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def greet():<\/p>\n<p>    print(&quot;Hello,&quot;)<\/p>\n<p>    await asyncio.sleep(1)<\/p>\n<p>    print(&quot;World!&quot;)<\/p>\n<p>asyncio.run(greet())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello,<\/p>\n<p>World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u51fd\u6570\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u6ce8\u89e3\u7528\u4e8e\u63d0\u4f9b\u6709\u5173\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u5143\u6570\u636e\u3002\u867d\u7136\u6ce8\u89e3\u4e0d\u4f1a\u5f71\u54cd\u51fd\u6570\u7684\u6267\u884c\uff0c\u4f46\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a: int, b: int) -&gt; int:<\/p>\n<p>    return a + b<\/p>\n<p>print(add.__annotations__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>{&#39;a&#39;: &lt;class &#39;int&#39;&gt;, &#39;b&#39;: &lt;class &#39;int&#39;&gt;, &#39;return&#39;: &lt;class &#39;int&#39;&gt;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u5e38\u89c1\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u800c\u4e0d\u9700\u8981\u5b9a\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>map()<\/h4>\n<\/p>\n<p><p><code>map()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u6307\u5b9a\u51fd\u6570\u5e94\u7528\u4e8e\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4]<\/p>\n<p>squared = map(lambda x: x * x, numbers)<\/p>\n<p>print(list(squared))  # \u8f93\u51fa [1, 4, 9, 16]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>filter()<\/h4>\n<\/p>\n<p><p><code>filter()<\/code>\u51fd\u6570\u7528\u4e8e\u8fc7\u6ee4\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5, 6]<\/p>\n<p>even_numbers = filter(lambda x: x % 2 == 0, numbers)<\/p>\n<p>print(list(even_numbers))  # \u8f93\u51fa [2, 4, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>reduce()<\/h4>\n<\/p>\n<p><p><code>reduce()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u5143\u7d20\u9010\u6b65\u5408\u5e76\u4e3a\u4e00\u4e2a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>numbers = [1, 2, 3, 4]<\/p>\n<p>sum = reduce(lambda x, y: x + y, numbers)<\/p>\n<p>print(sum)  # \u8f93\u51fa 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5f3a\u8c03\u4f7f\u7528\u51fd\u6570\u548c\u4e0d\u53ef\u53d8\u6570\u636e\u3002Python \u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u7279\u6027\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>\u9ad8\u9636\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u53ef\u4ee5\u63a5\u53d7\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def apply_twice(func, arg):<\/p>\n<p>    return func(func(arg))<\/p>\n<p>def add_five(x):<\/p>\n<p>    return x + 5<\/p>\n<p>result = apply_twice(add_five, 10)<\/p>\n<p>print(result)  # \u8f93\u51fa 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7eaf\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u7eaf\u51fd\u6570\u662f\u6307\u5728\u76f8\u540c\u8f93\u5165\u4e0b\u603b\u662f\u8fd4\u56de\u76f8\u540c\u8f93\u51fa\uff0c\u5e76\u4e14\u6ca1\u6709\u4efb\u4f55\u526f\u4f5c\u7528\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def pure_function(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>print(pure_function(3, 4))  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u907f\u514d\u526f\u4f5c\u7528<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u63d0\u5021\u907f\u514d\u526f\u4f5c\u7528\uff0c\u5373\u51fd\u6570\u4e0d\u5e94\u4fee\u6539\u5916\u90e8\u72b6\u6001\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u907f\u514d\u526f\u4f5c\u7528\u7684\u7eaf\u51fd\u6570<\/p>\n<p>def add_to_list(lst, item):<\/p>\n<p>    return lst + [item]<\/p>\n<p>numbers = [1, 2, 3]<\/p>\n<p>new_numbers = add_to_list(numbers, 4)<\/p>\n<p>print(new_numbers)  # \u8f93\u51fa [1, 2, 3, 4]<\/p>\n<p>print(numbers)  # \u8f93\u51fa [1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u51fd\u6570\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u7f13\u5b58\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u8c03\u7528\u7684\u7ed3\u679c\uff0c\u4ee5\u4fbf\u5728\u76f8\u540c\u8f93\u5165\u4e0b\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=100)<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    if n &lt; 2:<\/p>\n<p>        return n<\/p>\n<p>    return fibonacci(n-1) + fibonacci(n-2)<\/p>\n<p>print(fibonacci(50))  # \u8f93\u51fa 12586269025<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u548c\u4f7f\u7528Python\u51fd\u6570\u662f\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u91cd\u7528\u548c\u53ef\u7ef4\u62a4\u4ee3\u7801\u7684\u5173\u952e\u3002\u901a\u8fc7\u7406\u89e3\u51fd\u6570\u7684\u57fa\u672c\u6982\u5ff5\u3001\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u3001\u8fd4\u56de\u503c\u3001\u88c5\u9970\u5668\u3001\u751f\u6210\u5668\u548c\u5f02\u6b65\u51fd\u6570\u7b49\u9ad8\u7ea7\u7279\u6027\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u7f16\u5199\u51fa\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u79f0\u548c\u62ec\u53f7\u3002\u51fd\u6570\u4f53\u7f29\u8fdb\u4e66\u5199\uff0c\u53ef\u4ee5\u5305\u542b\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name):  \n    return f&quot;Hello, {name}!&quot;  \n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u53c2\u6570<code>name<\/code>\u5e76\u8fd4\u56de\u4e00\u4e2a\u95ee\u5019\u8bed\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u7684\u54ea\u4e9b\u53c2\u6570\u7c7b\u578b\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u53c2\u6570\u7c7b\u578b\uff0c\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\u3002\u4f4d\u7f6e\u53c2\u6570\u662f\u6700\u5e38\u89c1\u7684\uff0c\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u5728\u8c03\u7528\u51fd\u6570\u65f6\u6307\u5b9a\u53c2\u6570\u540d\u79f0\uff0c\u9ed8\u8ba4\u53c2\u6570\u53ef\u4ee5\u5728\u672a\u4f20\u5165\u503c\u65f6\u4f7f\u7528\u9884\u8bbe\u503c\uff0c\u800c\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def example_function(a, b=10, *args, **kwargs):  \n    print(a, b)  \n    print(args)  \n    print(kwargs)  \n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8c03\u7528\u4e00\u4e2a\u5df2\u5b9a\u4e49\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u8c03\u7528\u4e00\u4e2a\u5df2\u5b9a\u4e49\u7684\u51fd\u6570\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u51fd\u6570\u540d\u79f0\u5e76\u5728\u62ec\u53f7\u5185\u4f20\u5165\u6240\u9700\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u5b83\uff1a  <\/p>\n<pre><code class=\"language-python\">print(greet(&quot;Alice&quot;))  \n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u6839\u636e\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u786e\u4fdd\u4f20\u5165\u6b63\u786e\u6570\u91cf\u548c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u8fdb\u884c\uff1a\u5b9a\u4e49\u51fd\u6570\u4f7f\u7528def\u5173\u952e\u5b57\u3001\u51fd\u6570\u540d\u5e94\u6709\u610f\u4e49\u3001\u5305\u542b\u53c2\u6570\u548c\u8fd4\u56de [&hellip;]","protected":false},"author":3,"featured_media":1182019,"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\/1182012"}],"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=1182012"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182012\/revisions"}],"predecessor-version":[{"id":1182020,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182012\/revisions\/1182020"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182019"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}