{"id":870,"date":"2025-01-11T12:01:59","date_gmt":"2025-01-11T04:01:59","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=870"},"modified":"2025-01-11T12:01:59","modified_gmt":"2025-01-11T04:01:59","slug":"python%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%9a%e4%b9%89%e4%b8%8e%e4%bd%bf%e7%94%a8%ef%bc%9a%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b%e4%b8%8e%e7%a4%ba%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/11\/python%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%9a%e4%b9%89%e4%b8%8e%e4%bd%bf%e7%94%a8%ef%bc%9a%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b%e4%b8%8e%e7%a4%ba%e4%be%8b\/","title":{"rendered":"Python\u51fd\u6570\u7684\u5b9a\u4e49\u4e0e\u4f7f\u7528\uff1a\u8be6\u7ec6\u6559\u7a0b\u4e0e\u793a\u4f8b"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u6bb5\u53ef\u4ee5\u91cd\u590d\u8c03\u7528\u7684\u4ee3\u7801\u5757\uff0c\u5b83\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ec4\u7ec7\u4ee3\u7801\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002\u638c\u63e1Python\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u662f\u7f16\u5199\u9ad8\u6548\u3001\u6e05\u6670\u4ee3\u7801\u7684\u57fa\u7840\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u51fd\u6570\u7684\u5b9a\u4e49\u3001\u53c2\u6570\u4f20\u9012\u3001\u8fd4\u56de\u503c\u53ca\u76f8\u5173\u6982\u5ff5\uff0c\u5e76\u901a\u8fc7\u793a\u4f8b\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u51fd\u6570\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b9a\u4e49\u4e00\u4e2a\u57fa\u672c\u7684\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u51fd\u6570\u3002\u51fd\u6570\u7684\u5b9a\u4e49\u5305\u62ec\u51fd\u6570\u540d\u3001\u53c2\u6570\u5217\u8868\uff08\u53ef\u9009\uff09\u3001\u5192\u53f7\uff08<code>:<\/code>\uff09\u4ee5\u53ca\u51fd\u6570\u4f53\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 \u51fd\u6570\u7684\u57fa\u672c\u7ed3\u6784<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def function_name(parameters):\n    # \u51fd\u6570\u4f53\n    # \u6267\u884c\u4efb\u52a1\n    return result  # \u53ef\u9009\u7684\u8fd4\u56de\u503c<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>function_name<\/code> \u662f\u51fd\u6570\u7684\u540d\u5b57\u3002<\/li>\n\n\n\n<li><code>parameters<\/code> \u662f\u51fd\u6570\u7684\u8f93\u5165\uff0c\u53c2\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u6216\u6ca1\u6709\uff08\u51fd\u6570\u53ef\u4ee5\u6ca1\u6709\u53c2\u6570\uff09\u3002<\/li>\n\n\n\n<li><code>return<\/code> \u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u544a\u8bc9\u51fd\u6570\u6267\u884c\u7684\u7ed3\u679c\u3002<code>return<\/code> \u662f\u53ef\u9009\u7684\uff0c\u82e5\u6ca1\u6709<code>return<\/code>\uff0c\u51fd\u6570\u9ed8\u8ba4\u4e3a\u8fd4\u56de <code>None<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 \u793a\u4f8b\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    print(f\"Hello, {name}!\")\n\n# \u8c03\u7528\u51fd\u6570\ngreet(\"Alice\")  # \u8f93\u51fa\uff1aHello, Alice!<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u51fd\u6570\u53c2\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u4e0d\u540c\u7c7b\u578b\u7684\u53c2\u6570\u3002Python\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 \u4f4d\u7f6e\u53c2\u6570\uff08Positional Arguments\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u5e38\u89c1\u7684\u53c2\u6570\u4f20\u9012\u65b9\u5f0f\u662f\u4f4d\u7f6e\u53c2\u6570\u3002\u53c2\u6570\u5728\u51fd\u6570\u8c03\u7528\u65f6\u6309\u4f4d\u7f6e\u4f20\u9012\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add(a, b):\n    return a + b\n\n# \u4f4d\u7f6e\u4f20\u9012\u53c2\u6570\nresult = add(5, 3)  # a=5, b=3\nprint(result)  # \u8f93\u51fa\uff1a8<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 \u9ed8\u8ba4\u53c2\u6570\uff08Default Arguments\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u5728\u8c03\u7528\u65f6\u53ef\u4ee5\u4e0d\u4f20\u9012\u67d0\u4e9b\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name=\"Guest\"):\n    print(f\"Hello, {name}!\")\n\ngreet()  # \u8f93\u51fa\uff1aHello, Guest!\ngreet(\"Alice\")  # \u8f93\u51fa\uff1aHello, Alice!<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 \u53ef\u53d8\u53c2\u6570\uff08Variable-length Arguments\uff09<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u51fd\u6570\u4f1a\u63a5\u6536\u5230\u591a\u5c11\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u53ef\u53d8\u53c2\u6570\u3002\u53ef\u53d8\u53c2\u6570\u6709\u4e24\u79cd\u5f62\u5f0f\uff1a<code>*args<\/code>\uff08\u5143\u7ec4\u5f62\u5f0f\uff09\u548c<code>**kwargs<\/code>\uff08\u5b57\u5178\u5f62\u5f0f\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>*args<\/code> \u7528\u4e8e\u63a5\u6536\u591a\u4e2a\u4f4d\u7f6e\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u5143\u7ec4\u3002<\/li>\n\n\n\n<li><code>**kwargs<\/code> \u7528\u4e8e\u63a5\u6536\u591a\u4e2a\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u5b57\u5178\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528 *args\ndef sum_all(*args):\n    return sum(args)\n\nprint(sum_all(1, 2, 3, 4))  # \u8f93\u51fa\uff1a10\n\n# \u4f7f\u7528 **kwargs\ndef greet(**kwargs):\n    for key, value in kwargs.items():\n        print(f\"{key}: {value}\")\n\ngreet(name=\"Alice\", age=30)  \n# \u8f93\u51fa\uff1a\n# name: Alice\n# age: 30<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 \u53c2\u6570\u7684\u987a\u5e8f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u5fc5\u987b\u9075\u5faa\u7279\u5b9a\u7684\u987a\u5e8f\uff1a\u4f4d\u7f6e\u53c2\u6570 -&gt; \u9ed8\u8ba4\u53c2\u6570 -&gt; \u53ef\u53d8\u53c2\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def example(a, b=2, *args, c=3, **kwargs):\n    print(a, b, args, c, kwargs)\n\n# \u8c03\u7528\u793a\u4f8b\nexample(1, 4, 5, 6, c=7, d=8)\n# \u8f93\u51fa\uff1a1 4 (5, 6) 7 {'d': 8}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u51fd\u6570\u8fd4\u56de\u503c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5355\u4e2a\u503c\u6216\u591a\u4e2a\u503c\uff08\u4ee5\u5143\u7ec4\u7684\u5f62\u5f0f\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 \u8fd4\u56de\u5355\u4e2a\u503c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>def square(x):\n    return x * x\n\nresult = square(4)\nprint(result)  # \u8f93\u51fa\uff1a16<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 \u8fd4\u56de\u591a\u4e2a\u503c<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Python\u5141\u8bb8\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u8fd9\u4e9b\u503c\u4f1a\u88ab\u81ea\u52a8\u5c01\u88c5\u6210\u4e00\u4e2a\u5143\u7ec4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def math_operations(a, b):\n    return a + b, a - b, a * b, a \/ b\n\nresult = math_operations(6, 3)\nprint(result)  # \u8f93\u51fa\uff1a(9, 3, 18, 2.0)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u533f\u540d\u51fd\u6570\uff08Lambda\u51fd\u6570\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u51fd\u6570\u5b9a\u4e49\u65b9\u5f0f\uff0c\u53eb\u505a<strong>\u533f\u540d\u51fd\u6570<\/strong>\uff08\u6216<strong>Lambda\u51fd\u6570<\/strong>\uff09\u3002\u5b83\u4f7f\u7528<code>lambda<\/code>\u5173\u952e\u5b57\u521b\u5efa\uff0c\u4e0d\u9700\u8981\u663e\u5f0f\u5b9a\u4e49\u51fd\u6570\u540d\u3002\u901a\u5e38\u7528\u4e8e\u9700\u8981\u77ed\u5c0f\u51fd\u6570\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u8bed\u6cd5\uff1alambda \u53c2\u6570: \u8868\u8fbe\u5f0f\nsquare = lambda x: x * x\nprint(square(5))  # \u8f93\u51fa\uff1a25<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lambda\u51fd\u6570\u5e38\u7528\u4e8e\u6392\u5e8f\u3001\u8fc7\u6ee4\u7b49\u64cd\u4f5c\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528 lambda \u51fd\u6570\u6392\u5e8f\nnumbers = &#91;(1, 'one'), (3, 'three'), (2, 'two')]\nnumbers.sort(key=lambda x: x&#91;0])\nprint(numbers)  # \u8f93\u51fa\uff1a&#91;(1, 'one'), (2, 'two'), (3, 'three')]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u51fd\u6570\u7684\u4f5c\u7528\u57df<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Python\u4e2d\uff0c\u51fd\u6570\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u548c\u53ef\u8bbf\u95ee\u6027\u3002Python\u67094\u4e2a\u4f5c\u7528\u57df\uff1aLEGB\uff08Local, Enclosing, Global, Built-in\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Local<\/strong>\uff1a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>Enclosing<\/strong>\uff1a\u5305\u542b\u5f53\u524d\u51fd\u6570\u7684\u5916\u5c42\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>Global<\/strong>\uff1a\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u7684\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>Built-in<\/strong>\uff1aPython\u5185\u7f6e\u7684\u540d\u5b57\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 \u5c40\u90e8\u53d8\u91cf\u4e0e\u5168\u5c40\u53d8\u91cf<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10  # \u5168\u5c40\u53d8\u91cf\n\ndef function():\n    x = 20  # \u5c40\u90e8\u53d8\u91cf\n    print(x)\n\nfunction()  # \u8f93\u51fa\uff1a20\nprint(x)  # \u8f93\u51fa\uff1a10<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <code>global<\/code> \u5173\u952e\u5b57<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>global<\/code> \u5173\u952e\u5b57\u5141\u8bb8\u4f60\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10\n\ndef modify_global():\n    global x\n    x = 20\n\nmodify_global()\nprint(x)  # \u8f93\u51fa\uff1a20<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u51fd\u6570\u6587\u6863\u5b57\u7b26\u4e32\uff08Docstring\uff09<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python\u5141\u8bb8\u4e3a\u51fd\u6570\u6dfb\u52a0\u6587\u6863\u5b57\u7b26\u4e32\uff08Docstring\uff09\uff0c\u8fd9\u6709\u52a9\u4e8e\u751f\u6210\u5e2e\u52a9\u6587\u6863\u6216\u63d0\u4f9b\u51fd\u6570\u7684\u4f7f\u7528\u8bf4\u660e\u3002\u6587\u6863\u5b57\u7b26\u4e32\u662f\u51fd\u6570\u5b9a\u4e49\u4e4b\u540e\u7d27\u8ddf\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    \"\"\"\n    \u6253\u62db\u547c\u51fd\u6570\n    :param name: \u7528\u6237\u7684\u540d\u5b57\n    :return: None\n    \"\"\"\n    print(f\"Hello, {name}!\")\n\nhelp(greet)  # \u8f93\u51fa\uff1agreet\u51fd\u6570\u7684\u6587\u6863<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. \u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51fd\u6570\u5b9a\u4e49<\/strong>\uff1a\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u53c2\u6570\u4f20\u9012<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528\u4f4d\u7f6e\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570<code>*args<\/code>\u548c<code>**kwargs<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u8fd4\u56de\u503c<\/strong>\uff1a\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u5355\u4e2a\u503c\u6216\u591a\u4e2a\u503c\uff08\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff09\u3002<\/li>\n\n\n\n<li><strong>\u533f\u540d\u51fd\u6570<\/strong>\uff1a\u4f7f\u7528<code>lambda<\/code>\u5b9a\u4e49\u77ed\u5c0f\u7684\u533f\u540d\u51fd\u6570\u3002<\/li>\n\n\n\n<li><strong>\u4f5c\u7528\u57df<\/strong>\uff1a\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u901a\u5e38\u662f\u5c40\u90e8\u7684\uff0c<code>global<\/code>\u5173\u952e\u5b57\u7528\u4e8e\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\uff1a\u4e3a\u51fd\u6570\u6dfb\u52a0\u6ce8\u91ca\uff0c\u4fbf\u4e8e\u7406\u89e3\u548c\u4f7f\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u57fa\u7840\u6982\u5ff5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u7f16\u5199\u529f\u80fd\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u662f\u4e00\u6bb5\u53ef\u4ee5\u91cd\u590d\u8c03\u7528\u7684\u4ee3\u7801\u5757\uff0c\u5b83\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\u3002\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7ec4\u7ec7\u4ee3\u7801\u3001\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"predecessor-version":[{"id":871,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/870\/revisions\/871"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}