{"id":1035610,"date":"2024-12-31T12:00:19","date_gmt":"2024-12-31T04:00:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035610.html"},"modified":"2024-12-31T12:00:22","modified_gmt":"2024-12-31T04:00:22","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%bc%80%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035610.html","title":{"rendered":"python\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/c77649be-78d0-4d31-a956-451bba5e82a9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u51fd\u6570\" \/><\/p>\n<p><p> <strong>Python\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5f00\u548c\u8c03\u7528\u4e00\u4e2a\u51fd\u6570<\/strong>\uff0c<strong>\u5305\u62ec\u5b9a\u4e49\u51fd\u6570\u3001\u8c03\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u88c5\u9970\u5668\u7b49<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u66f4\u5168\u9762\u5730\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u548c\u4f7f\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u548c\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u8ddf\u51fd\u6570\u540d\u79f0\u548c\u53c2\u6570\u5217\u8868\u3002\u51fd\u6570\u4f53\u4f7f\u7528\u7f29\u8fdb\u6765\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8c03\u7528\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ea\u9700\u4f7f\u7528\u51fd\u6570\u540d\u79f0\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">message = greet(&quot;Alice&quot;)<\/p>\n<p>print(message)  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5185\u7f6e\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u5982<code>print()<\/code>\u3001<code>len()<\/code>\u3001<code>range()<\/code>\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u65e0\u9700\u5b9a\u4e49\u5373\u53ef\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(len(numbers))  # \u8f93\u51fa\uff1a5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>\u5185\u7f6e\u51fd\u6570\u662fPython\u8bed\u8a00\u7684\u4e00\u5927\u4f18\u52bf\uff0c\u5b83\u4eec\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u4f7f\u7528\u65b9\u4fbf\u3002\u4f8b\u5982\uff0c<code>len()<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u83b7\u5f97\u5e8f\u5217\u7684\u957f\u5ea6\uff0c\u800c\u65e0\u9700\u624b\u52a8\u904d\u5386\u6574\u4e2a\u5e8f\u5217\u3002\u6b64\u5916\uff0cPython\u5185\u7f6e\u51fd\u6570\u8fd8\u5305\u62ec\u6570\u5b66\u8fd0\u7b97\u3001\u7c7b\u578b\u8f6c\u6362\u3001\u5b57\u7b26\u4e32\u5904\u7406\u7b49\u591a\u79cd\u529f\u80fd\uff0c\u6781\u5927\u5730\u65b9\u4fbf\u4e86\u5f00\u53d1\u8005\u7684\u65e5\u5e38\u7f16\u7a0b\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001Lambda\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>Lambda\u8868\u8fbe\u5f0f\u662f\u521b\u5efa\u533f\u540d\u51fd\u6570\u7684\u4e00\u79cd\u7b80\u6d01\u65b9\u5f0f\u3002\u5b83\u4eec\u901a\u5e38\u7528\u4e8e\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add = lambda x, y: x + y<\/p>\n<p>print(add(5, 3))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u9ad8\u7ea7\u51fd\u6570\u64cd\u4f5c\uff0c\u7528\u4e8e\u5728\u4e0d\u4fee\u6539\u51fd\u6570\u5b9a\u4e49\u7684\u60c5\u51b5\u4e0b\u6269\u5c55\u51fd\u6570\u7684\u529f\u80fd\u3002\u88c5\u9970\u5668\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u589e\u5f3a\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling function: {func.__name__}&quot;)<\/p>\n<p>        return func(*args, kwargs)<\/p>\n<p>    return wrapper<\/p>\n<p>@log<\/p>\n<p>def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<p>print(multiply(2, 3))  # \u8f93\u51fa\uff1aCalling function: multiply  6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u51fd\u6570\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8d4b\u503c\u7ed9\u53d8\u91cf\u3001\u5b58\u50a8\u5728\u6570\u636e\u7ed3\u6784\u4e2d\u3001\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u3001\u8fd4\u56de\u51fd\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u8d4b\u503c\u7ed9\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def shout(text):<\/p>\n<p>    return text.upper()<\/p>\n<p>yell = shout<\/p>\n<p>print(yell(&quot;hello&quot;))  # \u8f93\u51fa\uff1aHELLO<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b58\u50a8\u5728\u6570\u636e\u7ed3\u6784\u4e2d<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def whisper(text):<\/p>\n<p>    return text.lower()<\/p>\n<p>funcs = [shout, whisper]<\/p>\n<p>for f in funcs:<\/p>\n<p>    print(f(&quot;Hello&quot;))  # \u8f93\u51fa\uff1aHELLO  hello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(func, name):<\/p>\n<p>    return func(f&quot;Hello, {name}&quot;)<\/p>\n<p>print(greet(shout, &quot;Alice&quot;))  # \u8f93\u51fa\uff1aHELLO, ALICE<\/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 make_adder(x):<\/p>\n<p>    def adder(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return adder<\/p>\n<p>add_five = make_adder(5)<\/p>\n<p>print(add_five(10))  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u201c\u8bb0\u4f4f\u201d\u521b\u5efa\u65f6\u7684\u73af\u5883\u3002\u95ed\u5305\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6570\u636e\u5c01\u88c5\u548c\u5de5\u5382\u51fd\u6570\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>closure = outer_func(10)<\/p>\n<p>print(closure(5))  # \u8f93\u51fa\uff1a15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u8fed\u4ee3\u5668\u3002\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u6bcf\u6b21\u8c03\u7528<code>next()<\/code>\u65f6\u751f\u6210\u4e00\u4e2a\u503c\u3002<\/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 num in countdown(5):<\/p>\n<p>    print(num)  # \u8f93\u51fa\uff1a5 4 3 2 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5f02\u6b65\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u51fd\u6570\u4f7f\u7528<code>async<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\uff0c\u7528\u4e8e\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u3002\u5f02\u6b65\u51fd\u6570\u9700\u8981\u4e0e<code>aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t<\/code>\u5173\u952e\u5b57\u914d\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def fetch_data():<\/p>\n<p>    await asyncio.sleep(2)<\/p>\n<p>    return &quot;Data fetched&quot;<\/p>\n<p>async def main():<\/p>\n<p>    data = await fetch_data()<\/p>\n<p>    print(data)<\/p>\n<p>asyncio.run(main())  # \u8f93\u51fa\uff1aData fetched<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u51fd\u6570\u3002\u8fd9\u79cd\u6280\u672f\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u3002<\/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\uff1a120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u9ad8\u9636\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002\u5e38\u89c1\u7684\u9ad8\u9636\u51fd\u6570\u5305\u62ec<code>map()<\/code>\u3001<code>filter()<\/code>\u3001<code>reduce()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>map()<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>squared = list(map(lambda x: x  2, numbers))<\/p>\n<p>print(squared)  # \u8f93\u51fa\uff1a[1, 4, 9, 16, 25]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>filter()<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">even = list(filter(lambda x: x % 2 == 0, numbers))<\/p>\n<p>print(even)  # \u8f93\u51fa\uff1a[2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>reduce()<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>product = reduce(lambda x, y: x * y, numbers)<\/p>\n<p>print(product)  # \u8f93\u51fa\uff1a120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u6a21\u5757\u548c\u5305\u4e2d\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u5b58\u50a8\u5728\u6a21\u5757\u548c\u5305\u4e2d\uff0c\u4ee5\u4fbf\u91cd\u7528\u3002\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u6216\u5305\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(math.sqrt(16))  # \u8f93\u51fa\uff1a4.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5bfc\u5165\u5305\u4e2d\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from os import path<\/p>\n<p>print(path.exists(&quot;example.txt&quot;))  # \u8f93\u51fa\uff1aTrue \u6216 False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u7c7b\u4e2d\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u5b9a\u4e49\u4e3a\u7c7b\u7684\u65b9\u6cd5\u3002\u65b9\u6cd5\u662f\u7c7b\u7684\u51fd\u6570\uff0c\u5b83\u4eec\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u901a\u5e38\u662f<code>self<\/code>\uff0c\u7528\u4e8e\u8868\u793a\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Greeter:<\/p>\n<p>    def __init__(self, name):<\/p>\n<p>        self.name = name<\/p>\n<p>    def greet(self):<\/p>\n<p>        return f&quot;Hello, {self.name}!&quot;<\/p>\n<p>greeter = Greeter(&quot;Alice&quot;)<\/p>\n<p>print(greeter.greet())  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7c7b\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @classmethod<\/p>\n<p>    def class_method(cls):<\/p>\n<p>        return &quot;This is a class method&quot;<\/p>\n<p>print(MyClass.class_method())  # \u8f93\u51fa\uff1aThis is a class method<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u9759\u6001\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    @staticmethod<\/p>\n<p>    def static_method():<\/p>\n<p>        return &quot;This is a static method&quot;<\/p>\n<p>print(MyClass.static_method())  # \u8f93\u51fa\uff1aThis is a static method<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u5e26\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4e3a\u53c2\u6570\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u8c03\u7528\u51fd\u6570\u65f6\u672a\u63d0\u4f9b\u8fd9\u4e9b\u53c2\u6570\u7684\u503c\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, greeting=&quot;Hello&quot;):<\/p>\n<p>    return f&quot;{greeting}, {name}!&quot;<\/p>\n<p>print(greet(&quot;Alice&quot;))  # \u8f93\u51fa\uff1aHello, Alice!<\/p>\n<p>print(greet(&quot;Bob&quot;, &quot;Hi&quot;))  # \u8f93\u51fa\uff1aHi, Bob!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u51fd\u6570\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u6ce8\u89e3\u662f\u4e00\u79cd\u4e3a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6dfb\u52a0\u5143\u6570\u636e\u7684\u65b9\u5f0f\u3002\u6ce8\u89e3\u4e0d\u4f1a\u5f71\u54cd\u51fd\u6570\u7684\u6267\u884c\uff0c\u4ec5\u7528\u4e8e\u6587\u6863\u548c\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(x: int, y: int) -&gt; int:<\/p>\n<p>    return x + y<\/p>\n<p>print(add(5, 3))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u89e3\u5305\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u8c03\u7528\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>*args<\/code>\u548c<code>&lt;strong&gt;kwargs<\/code>\u89e3\u5305\u53c2\u6570\u3002<code>*args<\/code>\u7528\u4e8e\u4f4d\u7f6e\u53c2\u6570\uff0c<code>&lt;\/strong&gt;kwargs<\/code>\u7528\u4e8e\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def foo(*args, kwargs):<\/p>\n<p>    print(&quot;args:&quot;, args)<\/p>\n<p>    print(&quot;kwargs:&quot;, kwargs)<\/p>\n<p>foo(1, 2, 3, a=4, b=5)  <\/p>\n<h2><strong>\u8f93\u51fa\uff1a<\/strong><\/h2>\n<h2><strong>args: (1, 2, 3)<\/strong><\/h2>\n<h2><strong>kwargs: {&#39;a&#39;: 4, &#39;b&#39;: 5}<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u662f\u51fd\u6570\u7684\u6587\u6863\u8bf4\u660e\uff0c\u901a\u5e38\u4f4d\u4e8e\u51fd\u6570\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;This function greets the person passed as parameter.&quot;&quot;&quot;<\/p>\n<p>    return f&quot;Hello, {name}!&quot;<\/p>\n<p>print(greet.__doc__)  # \u8f93\u51fa\uff1aThis function greets the person passed as parameter.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u51fd\u6570\u7f13\u5b58<\/h3>\n<\/p>\n<p><p>Python\u7684<code>functools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>lru_cache<\/code>\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u7f13\u5b58\u51fd\u6570\u7ed3\u679c\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=32)<\/p>\n<p>def fib(n):<\/p>\n<p>    if n &lt; 2:<\/p>\n<p>        return n<\/p>\n<p>    return fib(n-1) + fib(n-2)<\/p>\n<p>print(fib(10))  # \u8f93\u51fa\uff1a55<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516b\u3001\u7c7b\u578b\u68c0\u67e5<\/h3>\n<\/p>\n<p><p>Python\u7684<code>typing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7c7b\u578b\u63d0\u793a\uff0c\u7528\u4e8e\u9759\u6001\u7c7b\u578b\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from typing import List<\/p>\n<p>def sum_list(numbers: List[int]) -&gt; int:<\/p>\n<p>    return sum(numbers)<\/p>\n<p>print(sum_list([1, 2, 3, 4]))  # \u8f93\u51fa\uff1a10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e5d\u3001\u5f02\u6b65\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u751f\u6210\u5668\u7ed3\u5408\u4e86\u751f\u6210\u5668\u548c\u5f02\u6b65\u51fd\u6570\u7684\u7279\u6027\uff0c\u4f7f\u7528<code>async<\/code>\u548c<code>yield<\/code>\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">async def async_gen():<\/p>\n<p>    for i in range(3):<\/p>\n<p>        await asyncio.sleep(1)<\/p>\n<p>        yield i<\/p>\n<p>async def main():<\/p>\n<p>    async for value in async_gen():<\/p>\n<p>        print(value)<\/p>\n<p>asyncio.run(main())  # \u8f93\u51fa\uff1a0 1 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u5341\u3001\u7ec4\u5408\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u7ec4\u5408\u51fd\u6570\u662f\u5c06\u591a\u4e2a\u51fd\u6570\u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u5de5\u5177\u5b9e\u73b0\u7ec4\u5408\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>def compose(*funcs):<\/p>\n<p>    return reduce(lambda f, g: lambda x: f(g(x)), funcs)<\/p>\n<p>def double(x):<\/p>\n<p>    return x * 2<\/p>\n<p>def increment(x):<\/p>\n<p>    return x + 1<\/p>\n<p>composed_func = compose(double, increment)<\/p>\n<p>print(composed_func(3))  # \u8f93\u51fa\uff1a8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u5b9a\u4e49\u3001\u8c03\u7528\u548c\u7ba1\u7406\u51fd\u6570\uff0c\u8fdb\u800c\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u8bfb\u6027\u5f3a\u7684\u4ee3\u7801\u3002\u4e86\u89e3\u8fd9\u4e9b\u6280\u5de7\u548c\u65b9\u6cd5\u5c06\u6781\u5927\u5730\u63d0\u5347\u60a8\u7684Python\u7f16\u7a0b\u6c34\u5e73\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\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\u53c2\u6570\u5217\u8868\u3002\u6bd4\u5982\uff0c<code>def my_function(param1, param2):<\/code>\u3002\u51fd\u6570\u4f53\u9700\u8981\u7f29\u8fdb\uff0c\u53ef\u4ee5\u4f7f\u7528<code>return<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u7ed3\u679c\u3002\u4e3e\u4f8b\u6765\u8bf4\uff1a<\/p>\n<pre><code class=\"language-python\">def add(a, b):\n    return a + b\n<\/code><\/pre>\n<p>\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>add(3, 5)<\/code>\uff0c\u8fd4\u56de\u503c\u5c06\u662f8\u3002<\/p>\n<p><strong>Python\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u54ea\u4e9b\u7c7b\u578b\u7684\u53c2\u6570\uff1f<\/strong><br \/>Python\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u591a\u79cd\u7c7b\u578b\u7684\u53c2\u6570\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\u6307\u6309\u987a\u5e8f\u4f20\u5165\u7684\u53c2\u6570\uff0c\u5173\u952e\u5b57\u53c2\u6570\u5219\u662f\u901a\u8fc7<code>key=value<\/code>\u5f62\u5f0f\u4f20\u5165\u7684\u3002\u9ed8\u8ba4\u53c2\u6570\u662f\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u63d0\u4f9b\u7684\u9ed8\u8ba4\u503c\uff0c\u800c\u53ef\u53d8\u53c2\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f7f\u7528<code>*args<\/code>\u548c<code>**kwargs<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\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\u52a0\u4e0a\u5706\u62ec\u53f7\uff0c\u5e76\u5728\u62ec\u53f7\u5185\u4f20\u5165\u6240\u9700\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>multiply(a, b)<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7<code>multiply(4, 5)<\/code>\u6765\u8c03\u7528\u5b83\uff0c\u8fd4\u56de\u7ed3\u679c\u5c06\u662f20\u3002\u5982\u679c\u51fd\u6570\u6709\u9ed8\u8ba4\u53c2\u6570\uff0c\u672a\u4f20\u5165\u7684\u53c2\u6570\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5f00\u548c\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u5305\u62ec\u5b9a\u4e49\u51fd\u6570\u3001\u8c03\u7528\u5185\u7f6e\u51fd\u6570\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u3001\u5229\u7528\u88c5\u9970\u5668\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1035619,"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\/1035610"}],"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=1035610"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035610\/revisions"}],"predecessor-version":[{"id":1035622,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035610\/revisions\/1035622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035619"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}