{"id":1005017,"date":"2024-12-27T10:31:55","date_gmt":"2024-12-27T02:31:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005017.html"},"modified":"2024-12-27T10:31:58","modified_gmt":"2024-12-27T02:31:58","slug":"%e5%a6%82%e4%bd%95%e7%bb%99python%e5%87%bd%e6%95%b0%e4%bc%a0%e5%85%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005017.html","title":{"rendered":"\u5982\u4f55\u7ed9python\u51fd\u6570\u4f20\u5165"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081719\/372761c6-0d0f-4a75-a742-58e1b684b3f7.webp\" alt=\"\u5982\u4f55\u7ed9python\u51fd\u6570\u4f20\u5165\" \/><\/p>\n<p><p> \u4e00\u3001\u5982\u4f55\u7ed9Python\u51fd\u6570\u4f20\u5165\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u7ed9\u51fd\u6570\u4f20\u5165\u53c2\u6570\u6709\u591a\u79cd\u65b9\u5f0f<\/strong>\uff0c\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\u7b49\u3002\u5176\u4e2d\uff0c\u4f4d\u7f6e\u53c2\u6570\u6309\u7167\u987a\u5e8f\u4f20\u9012\uff0c\u5173\u952e\u5b57\u53c2\u6570\u901a\u8fc7\u53c2\u6570\u540d\u6307\u5b9a\uff0c\u9ed8\u8ba4\u53c2\u6570\u5219\u662f\u5728\u672a\u4f20\u9012\u65f6\u4f7f\u7528\u9884\u8bbe\u503c\uff0c\u800c\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u4f20\u5165\u4e0d\u5b9a\u6570\u91cf\u7684\u53c2\u6570\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5173\u952e\u5b57\u53c2\u6570\u7684\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u4f60\u5728\u8c03\u7528\u51fd\u6570\u65f6\u901a\u8fc7\u53c2\u6570\u540d\u79f0\u6307\u5b9a\u53c2\u6570\u503c\uff0c\u800c\u4e0d\u5fc5\u9075\u5faa\u4f4d\u7f6e\u987a\u5e8f\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u6613\u8bfb\u3002\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570 <code>def greet(name, message)<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>greet(name=&quot;Alice&quot;, message=&quot;Hello&quot;)<\/code> \u8c03\u7528\u3002\u5173\u952e\u5b57\u53c2\u6570\u7684\u597d\u5904\u5728\u4e8e\u5b83\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5e76\u51cf\u5c11\u4e86\u9519\u8bef\u7684\u53ef\u80fd\u6027\uff0c\u56e0\u4e3a\u53c2\u6570\u987a\u5e8f\u4e0d\u518d\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f4d\u7f6e\u53c2\u6570\u4e0e\u5173\u952e\u5b57\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u662f\u6700\u57fa\u672c\u7684\u4e24\u79cd\u4f20\u53c2\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f4d\u7f6e\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u6700\u5e38\u7528\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\uff0c\u53c2\u6570\u7684\u503c\u6309\u7167\u5b9a\u4e49\u65f6\u7684\u4f4d\u7f6e\u987a\u5e8f\u4f9d\u6b21\u4f20\u9012\u7ed9\u51fd\u6570\u4e2d\u7684\u53c2\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, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>3<\/code> \u548c <code>5<\/code> \u5206\u522b\u4f20\u9012\u7ed9 <code>a<\/code> \u548c <code>b<\/code>\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u952e\u5b57\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u901a\u8fc7\u53c2\u6570\u540d\u79f0\u4f20\u9012\u503c\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u7279\u522b\u662f\u5728\u51fd\u6570\u53c2\u6570\u8f83\u591a\u6216\u4e0d\u8bb0\u5f97\u53c2\u6570\u987a\u5e8f\u65f6\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(age=25, name=&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5373\u4f7f\u53c2\u6570\u987a\u5e8f\u4e0d\u540c\uff0c\u4e5f\u80fd\u51c6\u786e\u5730\u4f20\u9012\u7ed9\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u9ed8\u8ba4\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u53c2\u6570\u5728\u6ca1\u6709\u4f20\u9012\u503c\u65f6\u91c7\u7528\u9ed8\u8ba4\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u51fd\u6570\u7684\u7075\u6d3b\u6027\uff0c\u51cf\u5c11\u8c03\u7528\u65f6\u5fc5\u987b\u4f20\u9012\u7684\u53c2\u6570\u6570\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def power(base, exponent=2):<\/p>\n<p>    return base  exponent<\/p>\n<p>print(power(3))  # \u8f93\u51fa: 9\uff0c\u4f7f\u7528\u9ed8\u8ba4\u6307\u65702<\/p>\n<p>print(power(3, 3))  # \u8f93\u51fa: 27\uff0c\u4f7f\u7528\u6307\u5b9a\u6307\u65703<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>exponent<\/code> \u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a <code>2<\/code>\uff0c\u5982\u679c\u8c03\u7528\u65f6\u672a\u4f20\u9012\uff0c\u8be5\u53c2\u6570\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u53ef\u53d8\u53c2\u6570<\/p>\n<\/p>\n<p><p>\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u6709\u4e24\u79cd\u7c7b\u578b\uff1a*args \u548c kwargs\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f4d\u7f6e\u53ef\u53d8\u53c2\u6570 (*args)<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>*args<\/code> \u53ef\u4ee5\u63a5\u6536\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u51fd\u6570\u5185\u90e8\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\u5904\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_all(*args):<\/p>\n<p>    return sum(args)<\/p>\n<p>print(sum_all(1, 2, 3, 4))  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>sum_all<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u952e\u5b57\u53ef\u53d8\u53c2\u6570 (<\/strong>kwargs)<\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>kwargs<\/code> \u53ef\u4ee5\u63a5\u6536\u591a\u4e2a\u5173\u952e\u5b57\u53c2\u6570\uff0c\u51fd\u6570\u5185\u90e8\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5b57\u5178\u5904\u7406\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)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>print_info<\/code> \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u9010\u4e00\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u53c2\u6570\u7ec4\u5408\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u7ec4\u5408\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u4ee5\u6ee1\u8db3\u66f4\u590d\u6742\u7684\u9700\u6c42\u3002\u5e38\u89c1\u7684\u53c2\u6570\u7ec4\u5408\u987a\u5e8f\u662f\uff1a\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001*args \u548c kwargs\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def display_info(name, age=20, *args, kwargs):<\/p>\n<p>    print(f&quot;Name: {name}, Age: {age}&quot;)<\/p>\n<p>    print(&quot;Additional args:&quot;, args)<\/p>\n<p>    print(&quot;Additional kwargs:&quot;, kwargs)<\/p>\n<p>display_info(&quot;Alice&quot;, 25, &quot;Student&quot;, location=&quot;NYC&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u51fd\u6570 <code>display_info<\/code> \u540c\u65f6\u4f7f\u7528\u4e86\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001*args \u548c kwargs\uff0c\u4ee5\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u53c2\u6570\u89e3\u5305<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53c2\u6570\u89e3\u5305\u662f\u4e00\u79cd\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u7528\u4e8e\u5c06\u5e8f\u5217\u6216\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528 <code>*<\/code> \u548c <code><\/code> \u8fdb\u884c\u89e3\u5305\uff0c\u53ef\u4ee5\u5c06\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b, c):<\/p>\n<p>    return a + b + c<\/p>\n<p>numbers = [1, 2, 3]<\/p>\n<p>print(add(*numbers))  # \u8f93\u51fa: 6<\/p>\n<p>info = {&#39;a&#39;: 1, &#39;b&#39;: 2, &#39;c&#39;: 3}<\/p>\n<p>print(add(info))  # \u8f93\u51fa: 6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u901a\u8fc7 <code>*numbers<\/code> \u548c <code>info<\/code> \u5206\u522b\u5c06\u5217\u8868\u548c\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u4f20\u9012\u7ed9\u51fd\u6570 <code>add<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u53c2\u6570\u6ce8\u91ca\u4e0e\u7c7b\u578b\u63d0\u793a<\/p>\n<\/p>\n<p><p>Python\u652f\u6301\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u63d0\u793a\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u63d0\u793a\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u7f16\u5199\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u7684\u4ee3\u7801\u3002\u867d\u7136Python\u4e0d\u4f1a\u5f3a\u5236\u6267\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u4f46\u7c7b\u578b\u63d0\u793a\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u51cf\u5c11\u9519\u8bef\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name: str, age: int) -&gt; str:<\/p>\n<p>    return f&quot;Hello, my name is {name} and I am {age} years old.&quot;<\/p>\n<p>print(greet(&quot;Alice&quot;, 30))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u53c2\u6570 <code>name<\/code> \u548c <code>age<\/code> \u5206\u522b\u6807\u6ce8\u4e86 <code>str<\/code> \u548c <code>int<\/code> \u7c7b\u578b\uff0c\u5e76\u4e14\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e3a <code>str<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001Lambdas\u4e0e\u533f\u540d\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0clambda\u8868\u8fbe\u5f0f\u7528\u4e8e\u521b\u5efa\u533f\u540d\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u7b80\u5355\u51fd\u6570\u7684\u573a\u5408\u3002lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4f46\u53ea\u80fd\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u4e14\u8be5\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u5373\u4e3a\u8fd4\u56de\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add = lambda x, y: x + y<\/p>\n<p>print(add(3, 5))  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>add<\/code> \u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u88c5\u9970\u5668\u4e0e\u9ad8\u9636\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6269\u5c55\u6216\u4fee\u6539\u51fd\u6570\u7684\u884c\u4e3a\u3002\u9ad8\u9636\u51fd\u6570\u662f\u4ee5\u5176\u4ed6\u51fd\u6570\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u901a\u5e38\u7528\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u6d4b\u8bd5\u548c\u6743\u9650\u9a8c\u8bc1\u7b49\u573a\u666f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def logger(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling function {func.__name__}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Function {func.__name__} returned {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@logger<\/p>\n<p>def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>multiply(3, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>logger<\/code> \u88c5\u9970\u5668\u7528\u4e8e\u8bb0\u5f55 <code>multiply<\/code> \u51fd\u6570\u7684\u8c03\u7528\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u53c2\u6570\u4f20\u9012\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u53c2\u6570\u4f20\u9012\u4e2d\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u4e86\u89e3\uff0c\u4ee5\u907f\u514d\u6f5c\u5728\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u53ef\u53d8\u5bf9\u8c61\u4e0e\u4e0d\u53ef\u53d8\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53c2\u6570\u4f20\u9012\u662f\u901a\u8fc7\u5bf9\u8c61\u5f15\u7528\u8fdb\u884c\u7684\u3002\u5f53\u4f20\u9012\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u548c\u5b57\u5178\uff09\u65f6\uff0c\u51fd\u6570\u5185\u90e8\u7684\u4fee\u6539\u4f1a\u5f71\u54cd\u5230\u5916\u90e8\u5bf9\u8c61\uff1b\u800c\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u6574\u6570\u548c\u5b57\u7b26\u4e32\uff09\u5219\u4e0d\u4f1a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def modify_list(lst):<\/p>\n<p>    lst.append(4)<\/p>\n<p>numbers = [1, 2, 3]<\/p>\n<p>modify_list(numbers)<\/p>\n<p>print(numbers)  # \u8f93\u51fa: [1, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>numbers<\/code> \u5217\u8868\u5728\u51fd\u6570\u8c03\u7528\u540e\u88ab\u4fee\u6539\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u907f\u514d\u53ef\u53d8\u9ed8\u8ba4\u53c2\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\u65f6\uff0c\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u884c\u4e3a\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u53ea\u521d\u59cb\u5316\u4e00\u6b21\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_item(item, item_list=[]):<\/p>\n<p>    item_list.append(item)<\/p>\n<p>    return item_list<\/p>\n<p>print(add_item(1))  # \u8f93\u51fa: [1]<\/p>\n<p>print(add_item(2))  # \u8f93\u51fa: [1, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u7531\u4e8e <code>item_list<\/code> \u662f\u4e00\u4e2a\u53ef\u53d8\u5bf9\u8c61\uff0c\u540e\u7eed\u8c03\u7528\u4f1a\u5f71\u54cd\u5230\u4e4b\u524d\u7684\u7ed3\u679c\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u9ed8\u8ba4\u503c\u8bbe\u4e3a <code>None<\/code> \u5e76\u5728\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u521d\u59cb\u5316\u6765\u907f\u514d\u6b64\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u4f20\u9012\u51fd\u6570\u53c2\u6570\uff0c\u4f7f\u7528\u8fd9\u4e9b\u7279\u6027\u53ef\u4ee5\u7f16\u5199\u51fa\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u4ee3\u7801\u3002\u7406\u89e3\u5e76\u5408\u7406\u8fd0\u7528\u8fd9\u4e9b\u7279\u6027\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u8d28\u91cf\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f20\u9012\u53c2\u6570\u7ed9\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u6307\u5b9a\u53c2\u6570\u6765\u4f20\u9012\u53c2\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u4ee5\u53ca\u53ef\u53d8\u53c2\u6570\u7b49\u591a\u79cd\u65b9\u5f0f\u3002\u4f4d\u7f6e\u53c2\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\uff0c\u60a8\u53ea\u9700\u8981\u5728\u8c03\u7528\u51fd\u6570\u65f6\u6309\u7167\u987a\u5e8f\u63d0\u4f9b\u76f8\u5e94\u7684\u503c\u3002\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u60a8\u5728\u8c03\u7528\u65f6\u6307\u5b9a\u53c2\u6570\u540d\u79f0\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002\u9ed8\u8ba4\u53c2\u6570\u5219\u4e3a\u67d0\u4e9b\u53c2\u6570\u63d0\u4f9b\u4e86\u9ed8\u8ba4\u503c\uff0c\u4f7f\u5f97\u51fd\u6570\u5728\u672a\u63d0\u4f9b\u8fd9\u4e9b\u53c2\u6570\u65f6\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u3002\u53ef\u53d8\u53c2\u6570\u5219\u5141\u8bb8\u60a8\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u901a\u5e38\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u548c\u53cc\u661f\u53f7\uff08**\uff09\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u5e76\u4e0d\u9650\u5236\uff0c\u4f46\u60a8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u6ce8\u89e3\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u4e2d\u4e3a\u53c2\u6570\u6307\u5b9a\u7c7b\u578b\uff0c\u60a8\u53ef\u4ee5\u660e\u786e\u671f\u671b\u7684\u8f93\u5165\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u5176\u4ed6\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7406\u89e3\u51fd\u6570\u7684\u7528\u9014\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u5229\u7528\u7c7b\u578b\u68c0\u67e5\u5de5\u5177\u6765\u6355\u6349\u6f5c\u5728\u7684\u9519\u8bef\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>isinstance()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u5e76\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\u7c7b\u578b\u6267\u884c\u4e0d\u540c\u7684\u903b\u8f91\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u51fd\u6570\u4e2d\u4f20\u9012\u5217\u8868\u6216\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u6216\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9Python\u51fd\u6570\u3002\u76f4\u63a5\u5c06\u5217\u8868\u6216\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u5185\u90e8\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u64cd\u4f5c\u548c\u4fee\u6539\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f20\u9012\u7684\u662f\u5f15\u7528\u800c\u975e\u590d\u5236\uff0c\u56e0\u6b64\u5728\u51fd\u6570\u5185\u90e8\u5bf9\u5217\u8868\u6216\u5b57\u5178\u7684\u4fee\u6539\u4f1a\u5f71\u54cd\u539f\u59cb\u6570\u636e\u3002\u5982\u679c\u60a8\u4e0d\u5e0c\u671b\u539f\u59cb\u6570\u636e\u88ab\u66f4\u6539\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>copy()<\/code>\u65b9\u6cd5\u6765\u521b\u5efa\u526f\u672c\u3002\u6b64\u5916\uff0c\u5229\u7528\u53ef\u53d8\u53c2\u6570\u529f\u80fd\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u5217\u8868\u6216\u5b57\u5178\u5143\u7d20\uff0c\u4f7f\u51fd\u6570\u66f4\u52a0\u7075\u6d3b\u548c\u901a\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e00\u3001\u5982\u4f55\u7ed9Python\u51fd\u6570\u4f20\u5165\u53c2\u6570 \u5728Python\u4e2d\uff0c\u7ed9\u51fd\u6570\u4f20\u5165\u53c2\u6570\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4 [&hellip;]","protected":false},"author":3,"featured_media":1005028,"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\/1005017"}],"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=1005017"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005017\/revisions"}],"predecessor-version":[{"id":1005031,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005017\/revisions\/1005031"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005028"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}