{"id":1091476,"date":"2025-01-08T14:12:05","date_gmt":"2025-01-08T06:12:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1091476.html"},"modified":"2025-01-08T14:12:07","modified_gmt":"2025-01-08T06:12:07","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%87%bd%e6%95%b0%e5%8f%98%e5%8f%82%e6%95%b0%e7%9a%84%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1091476.html","title":{"rendered":"python\u5982\u4f55\u7528\u51fd\u6570\u53d8\u53c2\u6570\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203642\/08c021d2-7414-45c1-9d02-c69b12afe73d.webp\" alt=\"python\u5982\u4f55\u7528\u51fd\u6570\u53d8\u53c2\u6570\u7684\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u6765\u6539\u53d8\u53c2\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u8fd4\u56de\u65b0\u503c\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u66f4\u76f4\u89c2\u4e14\u4e0d\u6d89\u53ca\u5168\u5c40\u53d8\u91cf\u7684\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u548c\u5b57\u5178\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u76f4\u63a5\u4fee\u6539\u3002\u53ef\u53d8\u5bf9\u8c61\u7684\u5f15\u7528\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f20\u9012\uff0c\u56e0\u6b64\u5bf9\u5b83\u4eec\u7684\u4efb\u4f55\u4fee\u6539\u90fd\u4f1a\u53cd\u6620\u5728\u51fd\u6570\u5916\u90e8\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5217\u8868\u4f5c\u4e3a\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662fPython\u4e2d\u7684\u53ef\u53d8\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def modify_list(my_list):<\/p>\n<p>    my_list.append(4)<\/p>\n<p>    my_list[0] = 100<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>original_list = [1, 2, 3]<\/p>\n<p>modify_list(original_list)<\/p>\n<p>print(original_list)  # \u8f93\u51fa: [100, 2, 3, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>modify_list<\/code>\u51fd\u6570\u5bf9\u4f20\u5165\u7684\u5217\u8868\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u8fd9\u4e9b\u4fee\u6539\u5728\u51fd\u6570\u5916\u90e8\u4e5f\u662f\u53ef\u89c1\u7684\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u5b57\u5178\u4f5c\u4e3a\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u4e5f\u662f\u53ef\u53d8\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def modify_dict(my_dict):<\/p>\n<p>    my_dict[&#39;new_key&#39;] = &#39;new_value&#39;<\/p>\n<p>    my_dict[&#39;existing_key&#39;] = &#39;modified_value&#39;<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>original_dict = {&#39;existing_key&#39;: &#39;original_value&#39;}<\/p>\n<p>modify_dict(original_dict)<\/p>\n<p>print(original_dict)  # \u8f93\u51fa: {&#39;existing_key&#39;: &#39;modified_value&#39;, &#39;new_key&#39;: &#39;new_value&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7c7b\u4f3c\u5730\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>modify_dict<\/code>\u51fd\u6570\u5bf9\u4f20\u5165\u7684\u5b57\u5178\u8fdb\u884c\u4e86\u4fee\u6539\uff0c\u8fd9\u4e9b\u4fee\u6539\u5728\u51fd\u6570\u5916\u90e8\u4e5f\u662f\u53ef\u89c1\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u7528\u5230\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">global_var = 10<\/p>\n<p>def modify_global_var():<\/p>\n<p>    global global_var<\/p>\n<p>    global_var = 20<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>print(global_var)  # \u8f93\u51fa: 10<\/p>\n<p>modify_global_var()<\/p>\n<p>print(global_var)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>modify_global_var<\/code>\u51fd\u6570\u4f7f\u7528\u4e86<code>global<\/code>\u5173\u952e\u5b57\u58f0\u660e\u8981\u4fee\u6539\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u6210\u529f\u5730\u4fee\u6539\u4e86\u5168\u5c40\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8fd4\u56de\u65b0\u503c<\/h3>\n<\/p>\n<p><p>\u6700\u5e38\u89c1\u548c\u63a8\u8350\u7684\u65b9\u5f0f\u662f\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u503c\uff0c\u7136\u540e\u5728\u51fd\u6570\u8c03\u7528\u5904\u8fdb\u884c\u8d4b\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u4fdd\u8bc1\u51fd\u6570\u7684\u7eaf\u7cb9\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u8fd4\u56de\u65b0\u503c\u5e76\u8d4b\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def modify_value(value):<\/p>\n<p>    return value * 2<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>original_value = 10<\/p>\n<p>new_value = modify_value(original_value)<\/p>\n<p>print(new_value)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>modify_value<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a\u65b0\u7684\u503c\uff0c\u5e76\u5728\u8c03\u7528\u5904\u8fdb\u884c\u8d4b\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u76f4\u63a5\u4fee\u6539\u4f20\u5165\u7684\u53c2\u6570\uff0c\u4fdd\u6301\u51fd\u6570\u7684\u7eaf\u7cb9\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u7c7b\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u53c2\u6570\u503c\u7684\u4fee\u6539\u3002\u7c7b\u548c\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7ed3\u6784\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\u6765\u5904\u7406\u53c2\u6570\u7684\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">class Modifier:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def modify(self):<\/p>\n<p>        self.value *= 2<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>modifier = Modifier(10)<\/p>\n<p>print(modifier.value)  # \u8f93\u51fa: 10<\/p>\n<p>modifier.modify()<\/p>\n<p>print(modifier.value)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Modifier<\/code>\u7c7b\u6709\u4e00\u4e2a<code>modify<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4fee\u6539\u5b9e\u4f8b\u7684\u5c5e\u6027\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u66f4\u9002\u7528\u4e8e\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u9700\u8981\u591a\u6b21\u4fee\u6539\u53c2\u6570\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u901a\u8fc7\u51fd\u6570\u6765\u6539\u53d8\u53c2\u6570\u7684\u503c\uff1a<strong>\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09<\/strong>\u3001<strong>\u4f7f\u7528\u5168\u5c40\u53d8\u91cf<\/strong>\u3001<strong>\u8fd4\u56de\u65b0\u503c<\/strong>\u548c<strong>\u4f7f\u7528\u7c7b\u548c\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\u548c\u8fd4\u56de\u65b0\u503c\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u66f4\u7b26\u5408\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u601d\u60f3\u548c\u6700\u4f73\u5b9e\u8df5\u3002\u800c\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u867d\u7136\u53ef\u4ee5\u5b9e\u73b0\uff0c\u4f46\u4e0d\u63a8\u8350\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\u548c\u7ef4\u62a4\u96be\u5ea6\u3002\u901a\u8fc7\u7c7b\u548c\u65b9\u6cd5\u6765\u5b9e\u73b0\u53c2\u6570\u503c\u7684\u4fee\u6539\uff0c\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7ed3\u6784\u5316\u548c\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5e94\u7528\u573a\u666f\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\u53ef\u4ee5\u63a5\u53d7\u53ef\u53d8\u53c2\u6570\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u661f\u53f7\uff08*\uff09\u6765\u5b9a\u4e49\u53ef\u53d8\u53c2\u6570\u7684\u51fd\u6570\u3002\u901a\u8fc7\u5728\u53c2\u6570\u524d\u52a0\u4e0a\u661f\u53f7\uff0c\u51fd\u6570\u5c31\u80fd\u591f\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u4f1a\u88ab\u5c01\u88c5\u4e3a\u4e00\u4e2a\u5143\u7ec4\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function(*args):\n    for arg in args:\n        print(arg)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>my_function(1, 2, 3)<\/code>\u5c06\u8f93\u51fa1, 2, 3\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u5173\u952e\u5b57\u53ef\u53d8\u53c2\u6570\uff1f<\/strong><br \/>\u9664\u4e86\u4f4d\u7f6e\u53c2\u6570\uff0cPython\u8fd8\u652f\u6301\u5173\u952e\u5b57\u53ef\u53d8\u53c2\u6570\uff0c\u4f7f\u7528\u53cc\u661f\u53f7\uff08**\uff09\u6765\u5b9a\u4e49\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u4f60\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u4f1a\u88ab\u5c01\u88c5\u6210\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function(**kwargs):\n    for key, value in kwargs.items():\n        print(f&quot;{key}: {value}&quot;)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>my_function(name=&quot;Alice&quot;, age=30)<\/code>\u4f1a\u8f93\u51fa<code>name: Alice<\/code>\u548c<code>age: 30<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u51fd\u6570\u4e2d\u540c\u65f6\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u548c\u53ef\u53d8\u53c2\u6570\uff1f<\/strong><br \/>\u5728\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u3001\u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53ef\u53d8\u53c2\u6570\u3002\u9700\u8981\u6ce8\u610f\u5b83\u4eec\u7684\u987a\u5e8f\u3002\u4f4d\u7f6e\u53c2\u6570\u5e94\u653e\u5728\u6700\u524d\u9762\uff0c\u5176\u6b21\u662f\u53ef\u53d8\u53c2\u6570\uff0c\u6700\u540e\u662f\u5173\u952e\u5b57\u53ef\u53d8\u53c2\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function(a, b, *args, **kwargs):\n    print(a, b)\n    print(args)\n    print(kwargs)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>my_function(1, 2, 3, 4, name=&quot;Bob&quot;)<\/code>\u5c06\u8f93\u51fa<code>1 2<\/code>\uff0c<code>(3, 4)<\/code>\u548c<code>{&#39;name&#39;: &#39;Bob&#39;}<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684\u8f93\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u6765\u6539\u53d8\u53c2\u6570\u7684\u503c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a\u4f20\u5165\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\u3001\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3001\u8fd4 [&hellip;]","protected":false},"author":3,"featured_media":1091483,"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\/1091476"}],"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=1091476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091476\/revisions"}],"predecessor-version":[{"id":1091484,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1091476\/revisions\/1091484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1091483"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1091476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1091476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1091476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}