{"id":1111144,"date":"2025-01-08T17:26:23","date_gmt":"2025-01-08T09:26:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1111144.html"},"modified":"2025-01-08T17:26:26","modified_gmt":"2025-01-08T09:26:26","slug":"python%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e6%98%af%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e7%9a%84","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1111144.html","title":{"rendered":"python\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u7684"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073729\/4d2fe119-191b-40d6-9aa1-f000e4925a47.webp\" alt=\"python\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u662f\u5982\u4f55\u5b9a\u4e49\u7684\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u51fd\u6570\u53c2\u6570\u7684\u5b9a\u4e49\u662f\u975e\u5e38\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u3002<strong>\u51fd\u6570\u53c2\u6570\u5b9a\u4e49\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u53c2\u6570\u7c7b\u578b\uff0c\u5e76\u63a2\u8ba8\u5b83\u4eec\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f4d\u7f6e\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u4e2d\u6700\u5e38\u89c1\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u5728\u8c03\u7528\u51fd\u6570\u65f6\u5fc5\u987b\u6309\u987a\u5e8f\u4f20\u9012\u3002\u4f4d\u7f6e\u53c2\u6570\u7684\u987a\u5e8f\u51b3\u5b9a\u4e86\u6bcf\u4e2a\u53c2\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>print(add(3, 4))  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u662f\u4f4d\u7f6e\u53c2\u6570\uff0c\u8c03\u7528<code>add<\/code>\u51fd\u6570\u65f6\uff0c\u5fc5\u987b\u6309\u987a\u5e8f\u4f20\u9012\u8fd9\u4e24\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9ed8\u8ba4\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u5141\u8bb8\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u4e3a\u67d0\u4e9b\u53c2\u6570\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002\u5982\u679c\u5728\u8c03\u7528\u51fd\u6570\u65f6\u6ca1\u6709\u4f20\u9012\u8fd9\u4e9b\u53c2\u6570\u7684\u503c\uff0c\u51fd\u6570\u5c06\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 &quot;Hello, Alice!&quot;<\/p>\n<p>print(greet(&quot;Alice&quot;, &quot;Hi&quot;))  # \u8f93\u51fa &quot;Hi, Alice!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>greeting<\/code>\u662f\u4e00\u4e2a\u9ed8\u8ba4\u53c2\u6570\u3002\u5982\u679c\u8c03\u7528<code>greet<\/code>\u51fd\u6570\u65f6\u6ca1\u6709\u63d0\u4f9b<code>greeting<\/code>\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c&quot;Hello&quot;\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u5728\u8c03\u7528\u51fd\u6570\u65f6\u901a\u8fc7\u53c2\u6570\u540d\u6765\u4f20\u9012\u503c\uff0c\u4ece\u800c\u53ef\u4ee5\u4e0d\u6309\u987a\u5e8f\u4f20\u9012\u53c2\u6570\u3002\u5173\u952e\u5b57\u53c2\u6570\u4f7f\u5f97\u51fd\u6570\u8c03\u7528\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def describe_pet(animal_type, pet_name):<\/p>\n<p>    return f&quot;I have a {animal_type} named {pet_name}.&quot;<\/p>\n<p>print(describe_pet(animal_type=&quot;hamster&quot;, pet_name=&quot;Harry&quot;))  # \u8f93\u51fa &quot;I have a hamster named Harry.&quot;<\/p>\n<p>print(describe_pet(pet_name=&quot;Harry&quot;, animal_type=&quot;hamster&quot;))  # \u8f93\u51fa &quot;I have a hamster named Harry.&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>animal_type<\/code>\u548c<code>pet_name<\/code>\u662f\u5173\u952e\u5b57\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u540d\u4f20\u9012\u503c\uff0c\u987a\u5e8f\u65e0\u5173\u7d27\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u53ef\u53d8\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u6216\u5173\u952e\u5b57\u53c2\u6570\u3002\u53ef\u53d8\u53c2\u6570\u5206\u4e3a\u4e24\u79cd\uff1a*args\uff08\u4f4d\u7f6e\u53c2\u6570\uff09\u548ckwargs\uff08\u5173\u952e\u5b57\u53c2\u6570\uff09\u3002<\/p>\n<\/p>\n<p><h4>1. *args<\/h4>\n<\/p>\n<p><p>*args\u5141\u8bb8\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_pizza(size, *toppings):<\/p>\n<p>    print(f&quot;Making a {size}-inch pizza with the following toppings:&quot;)<\/p>\n<p>    for topping in toppings:<\/p>\n<p>        print(f&quot;- {topping}&quot;)<\/p>\n<p>make_pizza(12, &quot;pepperoni&quot;, &quot;mushrooms&quot;, &quot;green peppers&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>*toppings<\/code>\u5141\u8bb8\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u914d\u6599\uff0c\u51fd\u6570\u5c06\u8fd9\u4e9b\u914d\u6599\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><h4>2. kwargs<\/h4>\n<\/p>\n<p><p>kwargs\u5141\u8bb8\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u5178\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def build_profile(first, last, user_info):<\/p>\n<p>    profile = {}<\/p>\n<p>    profile[&#39;first_name&#39;] = first<\/p>\n<p>    profile[&#39;last_name&#39;] = last<\/p>\n<p>    for key, value in user_info.items():<\/p>\n<p>        profile[key] = value<\/p>\n<p>    return profile<\/p>\n<p>user_profile = build_profile(&#39;albert&#39;, &#39;einstein&#39;, location=&#39;princeton&#39;, field=&#39;physics&#39;)<\/p>\n<p>print(user_profile)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>user_info<\/code>\u5141\u8bb8\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u51fd\u6570\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u5178\u63a5\u6536\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u53c2\u6570\u7684\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53c2\u6570\u7684\u987a\u5e8f\u975e\u5e38\u91cd\u8981\u3002\u5fc5\u987b\u9075\u5faa\u4ee5\u4e0b\u987a\u5e8f\uff1a\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001*args\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001kwargs\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func(a, b=2, *args, d, kwargs):<\/p>\n<p>    print(a, b, args, d, kwargs)<\/p>\n<p>func(1, 3, 4, 5, d=6, e=7, f=8)  # \u8f93\u51fa 1 3 (4, 5) 6 {&#39;e&#39;: 7, &#39;f&#39;: 8}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53c2\u6570\u7684\u987a\u5e8f\u4f9d\u6b21\u4e3a\u4f4d\u7f6e\u53c2\u6570<code>a<\/code>\uff0c\u9ed8\u8ba4\u53c2\u6570<code>b<\/code>\uff0c\u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570<code>*args<\/code>\uff0c\u5173\u952e\u5b57\u53c2\u6570<code>d<\/code>\uff0c\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570<code>kwargs<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u53c2\u6570\u7c7b\u578b\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>Python \u8fd8\u5141\u8bb8\u4e3a\u51fd\u6570\u53c2\u6570\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u4e0d\u8fc7\uff0c\u7c7b\u578b\u6ce8\u89e3\u5e76\u4e0d\u4f1a\u5bf9\u53c2\u6570\u7c7b\u578b\u8fdb\u884c\u5f3a\u5236\u68c0\u67e5\uff0c\u5b83\u4eec\u4e3b\u8981\u7528\u4e8e\u6587\u6863\u548c\u4ee3\u7801\u63d0\u793a\u3002<\/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(3, 4))  # \u8f93\u51fa 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>a<\/code>\u548c<code>b<\/code>\u88ab\u6ce8\u89e3\u4e3a<code>int<\/code>\u7c7b\u578b\uff0c\u51fd\u6570\u8fd4\u56de\u503c\u4e5f\u88ab\u6ce8\u89e3\u4e3a<code>int<\/code>\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cPython\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u5b9a\u4e49\u662f\u975e\u5e38\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u3002<strong>\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570<\/strong>\u90fd\u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u51fd\u6570\u80fd\u591f\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u8c03\u7528\u573a\u666f\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u53c2\u6570\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u5b9a\u4e49\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u4e2d\u7684\u51fd\u6570\u53c2\u6570\uff1f<\/strong><br \/>Python\u4e2d\u7684\u51fd\u6570\u53c2\u6570\u662f\u6307\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u5141\u8bb8\u8f93\u5165\u7684\u53d8\u91cf\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684\u503c\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u4ece\u800c\u4f7f\u51fd\u6570\u66f4\u5177\u7075\u6d3b\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b9a\u4e49\u5177\u6709\u9ed8\u8ba4\u503c\u7684\u53c2\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\u8fd9\u6837\uff0c\u5f53\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u76f8\u5e94\u7684\u53c2\u6570\u503c\uff0c\u5219\u4f1a\u4f7f\u7528\u5b9a\u4e49\u65f6\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5199\u6210<code>def my_function(param1, param2=10):<\/code>\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>param2<\/code>\u7684\u9ed8\u8ba4\u503c\u4e3a10\u3002<\/p>\n<p><strong>\u53ef\u53d8\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/strong><br \/>\u53ef\u53d8\u53c2\u6570\uff08*args\uff09\u5141\u8bb8\u5c06\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u800c\u5173\u952e\u5b57\u53c2\u6570\uff08<strong>kwargs\uff09\u5219\u5141\u8bb8\u5c06\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\u4ee5\u5b57\u5178\u5f62\u5f0f\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u8fd9\u79cd\u7075\u6d3b\u7684\u53c2\u6570\u5b9a\u4e49\u65b9\u5f0f\u4f7f\u5f97\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u79cd\u7c7b\u578b\u548c\u6570\u91cf\u7684\u8f93\u5165\uff0c\u589e\u5f3a\u4e86\u51fd\u6570\u7684\u901a\u7528\u6027\u3002\u4f8b\u5982\uff0c<code>def my_function(*args, &lt;\/strong&gt;kwargs):<\/code>\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u548c\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u51fd\u6570\u53c2\u6570\u7684\u5b9a\u4e49\u662f\u975e\u5e38\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u3002\u51fd\u6570\u53c2\u6570\u5b9a\u4e49\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1111159,"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\/1111144"}],"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=1111144"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111144\/revisions"}],"predecessor-version":[{"id":1111162,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1111144\/revisions\/1111162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1111159"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1111144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1111144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1111144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}