{"id":1110922,"date":"2025-01-08T17:24:17","date_gmt":"2025-01-08T09:24:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110922.html"},"modified":"2025-01-08T17:24:21","modified_gmt":"2025-01-08T09:24:21","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e4%b8%80%e4%b8%aa%e6%96%b0%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110922.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073551\/1b82d838-816c-4ed3-b883-1ff10a3c571e.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u4f7f\u7528def\u5173\u952e\u5b57\u3001\u6307\u5b9a\u51fd\u6570\u540d\u3001\u5b9a\u4e49\u53c2\u6570\u3001\u7f16\u5199\u51fd\u6570\u4f53\u3001\u4f7f\u7528return\u8bed\u53e5\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f<strong>\u4f7f\u7528def\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u51fd\u6570<\/strong>\u3002\u5728\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u4e00\u6b65\u4e00\u6b65\u5730\u5b9a\u4e49\u548c\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684Python\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528def\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u51fd\u6570\u7684\u7b2c\u4e00\u6b65\u662f\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u3002<code>def<\/code>\u662fdefine\u7684\u7f29\u5199\uff0c\u5b83\u544a\u8bc9Python\u4f60\u6b63\u5728\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u3002\u7d27\u8ddf\u5728<code>def<\/code>\u540e\u9762\u7684\u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>my_function<\/code>\u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\u3002\u51fd\u6570\u540d\u5e94\u5f53\u7b80\u6d01\u660e\u4e86\uff0c\u5e76\u4e14\u9075\u5faa\u547d\u540d\u89c4\u5219\uff1a\u53ea\u80fd\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\uff0c\u4e14\u4e0d\u80fd\u4ee5\u6570\u5b57\u5f00\u5934\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6307\u5b9a\u51fd\u6570\u540d<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u540d\u5e94\u8be5\u80fd\u591f\u6e05\u6670\u5730\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\u3002\u597d\u7684\u51fd\u6570\u540d\u80fd\u591f\u8ba9\u4f60\u548c\u5176\u4ed6\u5f00\u53d1\u8005\u5728\u9605\u8bfb\u4ee3\u7801\u65f6\u66f4\u5bb9\u6613\u7406\u89e3\u4ee3\u7801\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u51fd\u6570\u662f\u7528\u6765\u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\uff0c\u53ef\u4ee5\u547d\u540d\u4e3a<code>calculate_sum<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum():<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9a\u4e49\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u7528\u4e8e\u5728\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u8ba1\u7b97\u3002\u53c2\u6570\u5728\u51fd\u6570\u540d\u540e\u9762\u7684\u62ec\u53f7\u4e2d\u5b9a\u4e49\uff0c\u53ef\u4ee5\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u3002\u5982\u679c\u6ca1\u6709\u53c2\u6570\uff0c\u62ec\u53f7\u5185\u7559\u7a7a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    pass<\/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>\u5c31\u662f\u51fd\u6570\u7684\u53c2\u6570\u3002\u4f60\u53ef\u4ee5\u4f20\u9012\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u7ed9\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199\u51fd\u6570\u4f53<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u4f53\u662f\u51fd\u6570\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u5305\u542b\u4e86\u51fd\u6570\u8981\u6267\u884c\u7684\u4ee3\u7801\u3002\u51fd\u6570\u4f53\u662f\u7f29\u8fdb\u7684\uff0c\u8fd9\u6837Python\u5c31\u77e5\u9053\u54ea\u4e9b\u4ee3\u7801\u662f\u5c5e\u4e8e\u51fd\u6570\u5185\u90e8\u7684\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u4f53\u4f1a\u5305\u542b\u4e00\u4e9b\u903b\u8f91\u8fd0\u7b97\u3001\u6761\u4ef6\u5224\u65ad\u6216\u5faa\u73af\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    result = a + b<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>result = a + b<\/code>\u5c31\u662f\u51fd\u6570\u4f53\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u8ba1\u7b97\u4e86\u4e24\u4e2a\u53c2\u6570\u7684\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528return\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><code>return<\/code>\u8bed\u53e5\u7528\u4e8e\u4ece\u51fd\u6570\u4e2d\u8fd4\u56de\u7ed3\u679c\u3002\u6ca1\u6709<code>return<\/code>\u8bed\u53e5\u7684\u51fd\u6570\u9ed8\u8ba4\u8fd4\u56de<code>None<\/code>\u3002<code>return<\/code>\u8bed\u53e5\u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    result = a + b<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>return result<\/code>\u5c06\u8ba1\u7b97\u7684\u7ed3\u679c\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8c03\u7528\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u597d\u51fd\u6570\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u5730\u65b9\u8c03\u7528\u5b83\u3002\u8c03\u7528\u51fd\u6570\u65f6\u9700\u8981\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\uff0c\u5982\u679c\u51fd\u6570\u6709\u53c2\u6570\u7684\u8bdd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sum_result = calculate_sum(5, 3)<\/p>\n<p>print(sum_result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>calculate_sum(5, 3)<\/code>\u8c03\u7528\u4e86\u6211\u4eec\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5e76\u4f20\u9012\u4e86\u4e24\u4e2a\u53c2\u6570<code>5<\/code>\u548c<code>3<\/code>\u3002\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u679c\u88ab\u5b58\u50a8\u5728\u53d8\u91cf<code>sum_result<\/code>\u4e2d\uff0c\u5e76\u901a\u8fc7<code>print<\/code>\u8bed\u53e5\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u66f4\u591a\u7684\u51fd\u6570\u7279\u6027<\/h3>\n<\/p>\n<p><h4>1\u3001\u9ed8\u8ba4\u53c2\u6570\u503c<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u6307\u5b9a\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 calculate_sum(a, b=10):<\/p>\n<p>    return a + b<\/p>\n<p>print(calculate_sum(5))  # \u8f93\u51fa 15<\/p>\n<p>print(calculate_sum(5, 3))  # \u8f93\u51fa 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u53c2\u6570<code>b<\/code>\u6709\u4e00\u4e2a\u9ed8\u8ba4\u503c<code>10<\/code>\u3002\u5982\u679c\u8c03\u7528\u51fd\u6570\u65f6\u6ca1\u6709\u4f20\u9012<code>b<\/code>\u7684\u503c\uff0c\u51fd\u6570\u5c06\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5173\u952e\u5b57\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u3002\u8fd9\u4f7f\u5f97\u51fd\u6570\u8c03\u7528\u66f4\u52a0\u6e05\u6670\uff0c\u5e76\u4e14\u53c2\u6570\u7684\u987a\u5e8f\u53ef\u4ee5\u968f\u610f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>print(calculate_sum(a=5, b=3))  # \u8f93\u51fa 8<\/p>\n<p>print(calculate_sum(b=3, a=5))  # \u8f93\u51fa 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570<code>a=5<\/code>\u548c<code>b=3<\/code>\u6765\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u53ef\u53d8\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u5e0c\u671b\u4e00\u4e2a\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(*args):<\/p>\n<p>    return sum(args)<\/p>\n<p>print(calculate_sum(1, 2, 3))  # \u8f93\u51fa 6<\/p>\n<p>print(calculate_sum(4, 5, 6, 7))  # \u8f93\u51fa 22<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>*args<\/code>\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5143\u7ec4\u4e2d\u3002<\/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, city=&quot;New York&quot;)<\/p>\n<h2><strong>\u8f93\u51fa<\/strong><\/h2>\n<h2><strong>name: Alice<\/strong><\/h2>\n<h2><strong>age: 30<\/strong><\/h2>\n<h2><strong>city: New York<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>kwargs<\/code>\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528\u5176\u81ea\u8eab\u7684\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\uff0c\u4f8b\u5982\u8ba1\u7b97\u9636\u4e58\u6216\u6590\u6ce2\u90a3\u5951\u6570\u5217\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\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>factorial<\/code>\u51fd\u6570\u662f\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u5b83\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u9636\u4e58\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u533f\u540d\u51fd\u6570\uff08lambda\uff09<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>lambda<\/code>\u5173\u952e\u5b57\u521b\u5efa\u533f\u540d\u51fd\u6570\u3002\u533f\u540d\u51fd\u6570\u662f\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u7b80\u5355\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">square = lambda x: x  2<\/p>\n<p>print(square(5))  # \u8f93\u51fa 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570<code>lambda x: x  2<\/code>\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u53d8\u91cf<code>square<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u5185\u7f6e\u51fd\u6570\u548c\u81ea\u5b9a\u4e49\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u81ea\u5b9a\u4e49\u51fd\u6570\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\uff0c\u5982<code>print<\/code>\u3001<code>len<\/code>\u3001<code>sum<\/code>\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u5185\u7f6e\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u7f16\u7a0b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>print(len(numbers))  # \u8f93\u51fa 5<\/p>\n<p>print(sum(numbers))  # \u8f93\u51fa 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5185\u7f6e\u51fd\u6570<code>len<\/code>\u548c<code>sum<\/code>\u6765\u8ba1\u7b97\u5217\u8868\u7684\u957f\u5ea6\u548c\u603b\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\uff08Docstring\uff09\u7528\u4e8e\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\u3002\u5b83\u901a\u5e38\u653e\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\uff0c\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7<code>&quot;&quot;&quot;<\/code>\u5305\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_sum(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8ba1\u7b97\u4e24\u4e2a\u6570\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a -- \u7b2c\u4e00\u4e2a\u6570<\/p>\n<p>    b -- \u7b2c\u4e8c\u4e2a\u6570<\/p>\n<p>    \u8fd4\u56de\u503c:<\/p>\n<p>    \u4e24\u4e2a\u6570\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4e3a<code>calculate_sum<\/code>\u51fd\u6570\u6dfb\u52a0\u4e86\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u63cf\u8ff0\u4e86\u51fd\u6570\u7684\u529f\u80fd\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u51fd\u6570\u7684\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u51b3\u5b9a\u4e86\u53d8\u91cf\u53ef\u4ee5\u5728\u54ea\u4e9b\u5730\u65b9\u88ab\u8bbf\u95ee\u3002\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\uff0c\u8fd9\u79f0\u4e3a\u5c40\u90e8\u4f5c\u7528\u57df\u3002\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\uff0c\u8fd9\u79f0\u4e3a\u5168\u5c40\u4f5c\u7528\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # \u5168\u5c40\u53d8\u91cf<\/p>\n<p>def my_function():<\/p>\n<p>    y = 5  # \u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(x)<\/p>\n<p>    print(y)<\/p>\n<p>my_function()<\/p>\n<p>print(x)<\/p>\n<h2><strong>print(y)  # \u8fd9\u884c\u4ee3\u7801\u4f1a\u5f15\u53d1\u9519\u8bef\uff0c\u56e0\u4e3ay\u662f\u5c40\u90e8\u53d8\u91cf<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>x<\/code>\u662f\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u548c\u5916\u90e8\u8bbf\u95ee\u3002\u800c<code>y<\/code>\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u51fd\u6570\u7684\u5d4c\u5957\u548c\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u5d4c\u5957\u5b9a\u4e49\uff0c\u5373\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u5d4c\u5957\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5176\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u8fd9\u79f0\u4e3a\u95ed\u5305\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(x):<\/p>\n<p>    def inner_function(y):<\/p>\n<p>        return x + y<\/p>\n<p>    return inner_function<\/p>\n<p>add_five = outer_function(5)<\/p>\n<p>print(add_five(3))  # \u8f93\u51fa 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code>\u662f\u4e00\u4e2a\u5d4c\u5957\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee<code>outer_function<\/code>\u7684\u53c2\u6570<code>x<\/code>\u3002<code>outer_function<\/code>\u8fd4\u56de\u4e86<code>inner_function<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>add_five<\/code>\u53d8\u91cf\u6765\u8c03\u7528\u8fd9\u4e2a\u95ed\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u901a\u5e38\u7528\u4e8e\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u7684\u884c\u4e3a\uff0c\u800c\u4e0d\u6539\u53d8\u51fd\u6570\u7684\u4ee3\u7801\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><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>my_decorator<\/code>\u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u5b83\u63a5\u53d7<code>say_hello<\/code>\u51fd\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570<code>wrapper<\/code>\u3002\u88c5\u9970\u5668\u901a\u8fc7\u5728\u51fd\u6570\u5b9a\u4e49\u524d\u4f7f\u7528<code>@<\/code>\u7b26\u53f7\u5e94\u7528\u4e8e\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u662f\u7f16\u7a0b\u7684\u57fa\u672c\u6280\u80fd\u3002\u901a\u8fc7\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u51fd\u6570\uff0c\u6307\u5b9a\u51fd\u6570\u540d\u3001\u53c2\u6570\u548c\u51fd\u6570\u4f53\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u529f\u80fd\u5f3a\u5927\u7684\u4ee3\u7801\u5757\u3002\u4e86\u89e3\u9ed8\u8ba4\u53c2\u6570\u503c\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u53ef\u53d8\u53c2\u6570\u3001\u9012\u5f52\u51fd\u6570\u3001\u533f\u540d\u51fd\u6570\u3001\u5185\u7f6e\u51fd\u6570\u3001\u6587\u6863\u5b57\u7b26\u4e32\u3001\u4f5c\u7528\u57df\u3001\u5d4c\u5957\u548c\u95ed\u5305\u4ee5\u53ca\u88c5\u9970\u5668\u7b49\u9ad8\u7ea7\u7279\u6027\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7f16\u5199\u66f4\u7075\u6d3b\u548c\u9ad8\u6548\u7684\u4ee3\u7801\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u7ecf\u9a8c\u4e30\u5bcc\u7684\u5f00\u53d1\u8005\uff0c\u638c\u63e1\u51fd\u6570\u7684\u4f7f\u7528\u90fd\u662f\u7f16\u5199\u9ad8\u8d28\u91cfPython\u4ee3\u7801\u7684\u5173\u952e\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\u4ee5\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u53ef\u4ee5\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u9762\u8ddf\u4e0a\u51fd\u6570\u540d\u548c\u53c2\u6570\u3002\u51fd\u6570\u4f53\u5185\u53ef\u4ee5\u7f16\u5199\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u4ee3\u7801\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">def my_function(parameter1, parameter2):\n    # \u51fd\u6570\u4f53\n    result = parameter1 + parameter2\n    return result\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5176\u4ed6\u5730\u65b9\u8c03\u7528\uff0c\u5e76\u4f20\u9012\u53c2\u6570\u8fdb\u884c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u7ed9Python\u51fd\u6570\u6dfb\u52a0\u9ed8\u8ba4\u53c2\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u5b9a\u4e49\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u5728\u8c03\u7528\u51fd\u6570\u65f6\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u4f20\u5165\u53c2\u6570\u3002\u5982\u679c\u672a\u4f20\u5165\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name=&quot;Guest&quot;):\n    print(&quot;Hello, &quot; + name)\n<\/code><\/pre>\n<p>\u8c03\u7528<code>greet()<\/code>\u5c06\u8f93\u51fa\u201cHello, Guest\u201d\uff0c\u800c\u8c03\u7528<code>greet(&quot;Alice&quot;)<\/code>\u5219\u8f93\u51fa\u201cHello, Alice\u201d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8fd4\u56de\u591a\u4e2a\u503c\uff1f<\/strong><br \/>Python\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u5143\u7ec4\u7684\u65b9\u5f0f\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u8fd9\u6837\u5728\u8c03\u7528\u51fd\u6570\u65f6\u53ef\u4ee5\u4e00\u6b21\u6027\u83b7\u53d6\u591a\u4e2a\u7ed3\u679c\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def calculate(x, y):\n    return x + y, x * y\n<\/code><\/pre>\n<p>\u8c03\u7528<code>calculate(2, 3)<\/code>\u5c06\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4<code>(5, 6)<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u89e3\u5305\u7684\u65b9\u5f0f\u83b7\u53d6\u8fd9\u4e24\u4e2a\u503c\uff1a<code>sum_result, product_result = calculate(2, 3)<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u4f7f\u7528def\u5173\u952e\u5b57\u3001\u6307\u5b9a\u51fd\u6570\u540d\u3001\u5b9a\u4e49\u53c2\u6570\u3001\u7f16\u5199\u51fd\u6570\u4f53\u3001\u4f7f\u7528r [&hellip;]","protected":false},"author":3,"featured_media":1110928,"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\/1110922"}],"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=1110922"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110922\/revisions"}],"predecessor-version":[{"id":1110929,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110922\/revisions\/1110929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110928"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}