{"id":980403,"date":"2024-12-27T06:54:09","date_gmt":"2024-12-26T22:54:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/980403.html"},"modified":"2024-12-27T06:54:11","modified_gmt":"2024-12-26T22:54:11","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/980403.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u6dfb\u52a0\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205812\/5dccb330-d709-4607-8b44-4c20c23d996b.webp\" alt=\"python\u4e2d\u5982\u4f55\u6dfb\u52a0\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u6dfb\u52a0\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u201cdef\u201d\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\u3001\u5c06\u51fd\u6570\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa\u533f\u540d\u51fd\u6570<\/strong>\u3002\u5b9a\u4e49\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u4ee3\u7801\u6e05\u6670\u6613\u8bfb\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u540e\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u5b83\uff0c\u5927\u5927\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u590d\u7528\u6027\u548c\u53ef\u8bfb\u6027\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u63d0\u4f9b\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u4ee5\u4fbf\u5728Python\u7a0b\u5e8f\u4e2d\u66f4\u6709\u6548\u5730\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u662fPython\u4e2d\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u7684\u57fa\u672c\u65b9\u5f0f\u3002\u901a\u8fc7\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u4e00\u7ec4\u7279\u5b9a\u7684\u64cd\u4f5c\u521b\u5efa\u4e00\u4e2a\u547d\u540d\u5757\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u51fd\u6570\u5b9a\u4e49\u8bed\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function_name(parameters):<\/p>\n<p>    &quot;&quot;&quot;docstring&quot;&quot;&quot;<\/p>\n<p>    statement(s)<\/p>\n<p>    return [expression]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>function_name<\/strong>: \u8fd9\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0c\u7528\u4e8e\u6807\u8bc6\u51fd\u6570\uff0c\u8c03\u7528\u51fd\u6570\u65f6\u4f7f\u7528\u3002<\/li>\n<li><strong>parameters<\/strong>: \u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u7528\u9017\u53f7\u5206\u9694\u3002<\/li>\n<li><strong>docstring<\/strong>: \u63cf\u8ff0\u51fd\u6570\u7528\u9014\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u4e5f\u662f\u53ef\u9009\u7684\u3002<\/li>\n<li><strong>statement(s)<\/strong>: \u51fd\u6570\u4f53\uff0c\u5305\u542b\u6267\u884c\u7684\u4ee3\u7801\u3002<\/li>\n<li><strong>return [expression]<\/strong>: \u53ef\u9009\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name):<\/p>\n<p>    &quot;&quot;&quot;\u663e\u793a\u7b80\u5355\u7684\u95ee\u5019\u8bed&quot;&quot;&quot;<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>greet<\/code>\u662f\u51fd\u6570\u540d\uff0c<code>name<\/code>\u662f\u53c2\u6570\uff0c\u51fd\u6570\u6253\u5370\u4e00\u6761\u95ee\u5019\u8bed\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u53c2\u6570<\/h3>\n<\/p>\n<p><p>Python\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u63a5\u6536\u8f93\u5165\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u662f\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u957f\u53c2\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f4d\u7f6e\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u6700\u5e38\u7528\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5b9e\u53c2\u6309\u7167\u4f4d\u7f6e\u4f20\u9012\u7ed9\u5f62\u53c2\u3002<\/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><h4>2\u3001\u9ed8\u8ba4\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u5141\u8bb8\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u4e3a\u53c2\u6570\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8c03\u7528\u65f6\u53ef\u4ee5\u7701\u7565\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message=&quot;Hello&quot;):<\/p>\n<p>    print(f&quot;{message}, {name}!&quot;)<\/p>\n<p>greet(&quot;Alice&quot;)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p>greet(&quot;Bob&quot;, &quot;Hi&quot;)  # \u8f93\u51fa: Hi, Bob!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u53ef\u53d8\u957f\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u53d8\u957f\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u4f7f\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u6765\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(*args):<\/p>\n<p>    return sum(args)<\/p>\n<p>print(add(1, 2, 3))  # \u8f93\u51fa: 6<\/p>\n<p>def display_info(kwargs):<\/p>\n<p>    for key, value in kwargs.items():<\/p>\n<p>        print(f&quot;{key}: {value}&quot;)<\/p>\n<p>display_info(name=&quot;Alice&quot;, age=25)  # \u8f93\u51fa: name: Alice, age: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u51fd\u6570\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\u6216\u591a\u4e2a\u503c\uff0c\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8fd4\u56de\u5355\u4e2a\u503c<\/h4>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>result = square(5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fd4\u56de\u591a\u4e2a\u503c<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0c\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def swap(a, b):<\/p>\n<p>    return b, a<\/p>\n<p>x, y = swap(1, 2)<\/p>\n<p>print(x, y)  # \u8f93\u51fa: 2 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u533f\u540d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u662f\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u64cd\u4f5c\u3002Python\u4e2d\u4f7f\u7528<code>lambda<\/code>\u5173\u952e\u5b57\u521b\u5efa\u533f\u540d\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u533f\u540d\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u7684\u8bed\u6cd5\u4e3a\uff1a<code>lambda \u53c2\u6570: \u8868\u8fbe\u5f0f<\/code>\u3002<\/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><h4>2\u3001\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u533f\u540d\u51fd\u6570\u5e38\u7528\u4e8e\u6392\u5e8f\u3001\u8fc7\u6ee4\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [(2, 2), (3, 4), (1, 3)]<\/p>\n<p>sorted_numbers = sorted(numbers, key=lambda x: x[1])<\/p>\n<p>print(sorted_numbers)  # \u8f93\u51fa: [(2, 2), (1, 3), (3, 4)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5bfc\u5165\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u6a21\u5757\u662f\u4e00\u79cd\u7ec4\u7ec7\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u51fd\u6570\u53ef\u4ee5\u88ab\u5b9a\u4e49\u5728\u6a21\u5757\u4e2d\uff0c\u5e76\u5728\u5176\u4ed6\u5730\u65b9\u5bfc\u5165\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5c06\u51fd\u6570\u5b9a\u4e49\u5728\u4e00\u4e2a.py\u6587\u4ef6\u4e2d\uff0c\u4f8b\u5982<code>mymodule.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># mymodule.py<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5728\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>import mymodule<\/p>\n<p>mymodule.greet(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>from ... import ...<\/code>\u8bed\u53e5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mymodule import greet<\/p>\n<p>greet(&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u4e00\u79cd\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u51fd\u6570\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u9012\u5f52\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u5fc5\u987b\u6709\u4e00\u4e2a\u57fa\u51c6\u6761\u4ef6\uff0c\u4ee5\u9632\u6b62\u65e0\u9650\u9012\u5f52\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/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><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>factorial<\/code>\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u8ba1\u7b97\u9636\u4e58\uff0c\u57fa\u51c6\u6761\u4ef6\u4e3a<code>n == 0<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u9ad8\u7ea7\u529f\u80fd\u3002\u88c5\u9970\u5668\u901a\u5e38\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u6d4b\u91cf\u3001\u4e8b\u52a1\u7ba1\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u88c5\u9970\u5668<\/h4>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper():<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        func()<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/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><h4>2\u3001\u88c5\u9970\u5668\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>my_decorator<\/code>\u662f\u88c5\u9970\u5668\uff0c<code>say_hello<\/code>\u51fd\u6570\u5728\u8c03\u7528\u65f6\u4f1a\u81ea\u52a8\u5e94\u7528\u88c5\u9970\u5668\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u7ec4\u7ec7\u4ee3\u7801\u3001\u5b9e\u73b0\u4ee3\u7801\u590d\u7528\u7684\u6838\u5fc3\u5de5\u5177\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3001\u521b\u5efa\u533f\u540d\u51fd\u6570\u3001\u5bfc\u5165\u6a21\u5757\u3001\u5b9e\u73b0\u9012\u5f52\u4ee5\u53ca\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u7a0b\u5e8f\u5458\u53ef\u4ee5\u6784\u5efa\u7075\u6d3b\u4e14\u9ad8\u6548\u7684\u7a0b\u5e8f\u7ed3\u6784\u3002\u7406\u89e3\u8fd9\u4e9b\u6982\u5ff5\u5e76\u719f\u7ec3\u8fd0\u7528\u5b83\u4eec\uff0c\u5c06\u5927\u5927\u63d0\u5347Python\u7f16\u7a0b\u7684\u80fd\u529b\u548c\u6548\u7387\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\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u51fd\u6570\u7684\u57fa\u672c\u7ed3\u6784\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\u548c\u51fd\u6570\u4f53\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u52a0\u6cd5\u51fd\u6570\u5982\u4e0b\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\u53ea\u9700\u4f7f\u7528<code>add(2, 3)<\/code>\uff0c\u5c06\u8fd4\u56de5\u3002<\/p>\n<p><strong>Python\u4e2d\u7684\u51fd\u6570\u53ef\u4ee5\u6709\u9ed8\u8ba4\u53c2\u6570\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u652f\u6301\u9ed8\u8ba4\u53c2\u6570\u3002\u60a8\u53ef\u4ee5\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\u8fd9\u610f\u5473\u7740\u5f53\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5982\u679c\u672a\u63d0\u4f9b\u67d0\u4e2a\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def greet(name=&quot;World&quot;):\n    return f&quot;Hello, {name}!&quot;\n<\/code><\/pre>\n<p>\u8c03\u7528<code>greet()<\/code>\u5c06\u8fd4\u56de&quot;Hello, World!&quot;\uff0c\u800c\u8c03\u7528<code>greet(&quot;Alice&quot;)<\/code>\u5219\u8fd4\u56de&quot;Hello, Alice!&quot;\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4f20\u9012\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>*args<\/code>\u548c<code>&lt;strong&gt;kwargs<\/code>\u6765\u4f20\u9012\u53ef\u53d8\u6570\u91cf\u7684\u53c2\u6570\u3002<code>*args<\/code>\u7528\u4e8e\u4f20\u9012\u975e\u5173\u952e\u5b57\u53c2\u6570\u7684\u5143\u7ec4\uff0c\u800c<code>&lt;\/strong&gt;kwargs<\/code>\u7528\u4e8e\u4f20\u9012\u5173\u952e\u5b57\u53c2\u6570\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">def display_info(*args, **kwargs):\n    print(&quot;Args:&quot;, args)\n    print(&quot;Kwargs:&quot;, kwargs)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>display_info(1, 2, 3, name=&quot;Alice&quot;, age=30)<\/code>\u5c06\u8f93\u51fa\uff1a<\/p>\n<pre><code>Args: (1, 2, 3)\nKwargs: {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u6dfb\u52a0\u51fd\u6570\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u201cdef\u201d\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\u3001\u5c06\u51fd\u6570\u4f5c\u4e3a\u6a21\u5757\u5bfc\u5165\u3001\u4f7f\u7528lambda\u8868\u8fbe\u5f0f\u521b\u5efa [&hellip;]","protected":false},"author":3,"featured_media":980410,"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\/980403"}],"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=980403"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980403\/revisions"}],"predecessor-version":[{"id":980412,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/980403\/revisions\/980412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/980410"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=980403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=980403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=980403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}