{"id":1163482,"date":"2025-01-15T14:55:40","date_gmt":"2025-01-15T06:55:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163482.html"},"modified":"2025-01-15T14:55:43","modified_gmt":"2025-01-15T06:55:43","slug":"python%e5%ae%9a%e4%b9%89%e5%87%bd%e6%95%b0%e5%90%8e%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1163482.html","title":{"rendered":"python\u5b9a\u4e49\u51fd\u6570\u540e\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204230\/8f896724-e1a5-42d2-8d0f-153cc4ec3fa6.webp\" alt=\"python\u5b9a\u4e49\u51fd\u6570\u540e\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Python\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u4f7f\u7528\u3001\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u51fd\u6570\u540d\u540e\u8ddf\u4e00\u5bf9\u5706\u62ec\u53f7<\/strong><\/p>\n<\/p>\n<p><p>\u51fd\u6570\u662fPython\u4e2d\u7684\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u5b83\u4eec\u5141\u8bb8\u4f60\u5c06\u4ee3\u7801\u5757\u5c01\u88c5\u5728\u4e00\u4e2a\u5355\u4e00\u7684\u3001\u53ef\u91cd\u7528\u7684\u547d\u540d\u5355\u5143\u4e2d\u3002\u51fd\u6570\u7684\u5b9a\u4e49\u548c\u8c03\u7528\u662fPython\u7f16\u7a0b\u4e2d\u7684\u6838\u5fc3\u90e8\u5206\u3002\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528Python\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51fd\u6570\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u901a\u8fc7<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def function_name(parameters):<\/p>\n<p>    &quot;&quot;&quot;docstring&quot;&quot;&quot;<\/p>\n<p>    # function body<\/p>\n<p>    return value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>function_name<\/code>\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0c<code>parameters<\/code>\u662f\u51fd\u6570\u7684\u53c2\u6570\uff0c<code>docstring<\/code>\u662f\u53ef\u9009\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\uff0c<code>function body<\/code>\u662f\u51fd\u6570\u7684\u4ee3\u7801\u5757\uff0c<code>value<\/code>\u662f\u51fd\u6570\u8fd4\u56de\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u4e4b\u548c\u7684\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;This function returns the sum of two numbers.&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u51fd\u6570\u7684\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u4f7f\u7528\u5b83\u3002\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>function_name(arguments)<\/code>\uff0c\u5176\u4e2d<code>function_name<\/code>\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0c<code>arguments<\/code>\u662f\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u7ee7\u7eed\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528<code>add<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = add(3, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u51fd\u6570\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u6709\u591a\u4e2a\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u6ca1\u6709\u53c2\u6570\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<\/p>\n<\/p>\n<p><h4>1\u3001\u4f4d\u7f6e\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u65f6\u7684\u5e38\u89c4\u53c2\u6570\uff0c\u8c03\u7528\u65f6\u6309\u987a\u5e8f\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<p>result = multiply(4, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5173\u952e\u5b57\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u5728\u8c03\u7528\u51fd\u6570\u65f6\u6307\u5b9a\u53c2\u6570\u7684\u540d\u79f0\uff0c\u4ece\u800c\u65e0\u9700\u6309\u987a\u5e8f\u4f20\u9012\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message):<\/p>\n<p>    return f&quot;{message}, {name}!&quot;<\/p>\n<p>result = greet(name=&quot;Alice&quot;, message=&quot;Hello&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9ed8\u8ba4\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u8d4b\u4e88\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u8c03\u7528\u65f6\u4e0d\u4f20\u9012\u8be5\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message=&quot;Hello&quot;):<\/p>\n<p>    return f&quot;{message}, {name}!&quot;<\/p>\n<p>result = greet(&quot;Alice&quot;)<\/p>\n<p>print(result)  # \u8f93\u51fa: Hello, Alice!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u53ef\u53d8\u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u53d8\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\u3002\u7528<code>*args<\/code>\u548c<code>kwargs<\/code>\u5206\u522b\u8868\u793a\u53ef\u53d8\u4f4d\u7f6e\u53c2\u6570\u548c\u53ef\u53d8\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(*args):<\/p>\n<p>    return sum(args)<\/p>\n<p>result = add(1, 2, 3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa: 10<\/p>\n<p>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><h3>\u56db\u3001\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u4e5f\u53ef\u4ee5\u8fd4\u56de\u591a\u4e2a\u503c\u3002\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u8fd4\u56de\u4e00\u4e2a\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>result = square(5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8fd4\u56de\u591a\u4e2a\u503c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def get_name_and_age():<\/p>\n<p>    name = &quot;Alice&quot;<\/p>\n<p>    age = 30<\/p>\n<p>    return name, age<\/p>\n<p>name, age = get_name_and_age()<\/p>\n<p>print(name)  # \u8f93\u51fa: Alice<\/p>\n<p>print(age)   # \u8f93\u51fa: 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u533f\u540d\u51fd\u6570<\/h3>\n<\/p>\n<p><p>Python\u8fd8\u652f\u6301\u533f\u540d\u51fd\u6570\uff0c\u4f7f\u7528<code>lambda<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u3002\u533f\u540d\u51fd\u6570\u901a\u5e38\u7528\u4e8e\u7b80\u77ed\u7684\u3001\u4e00\u6b21\u6027\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">square = lambda x: x * x<\/p>\n<p>result = square(5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u3002\u8fd9\u4f7f\u5f97\u51fd\u6570\u7684\u4f7f\u7528\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def apply_function(func, value):<\/p>\n<p>    return func(value)<\/p>\n<p>def square(x):<\/p>\n<p>    return x * x<\/p>\n<p>result = apply_function(square, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 25<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u95ed\u5305<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u51fd\u6570\u53ef\u4ee5\u5f15\u7528\u5176\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e26\u6709\u5c01\u88c5\u6570\u636e\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def outer_function(msg):<\/p>\n<p>    def inner_function():<\/p>\n<p>        print(msg)<\/p>\n<p>    return inner_function<\/p>\n<p>my_func = outer_function(&quot;Hello&quot;)<\/p>\n<p>my_func()  # \u8f93\u51fa: Hello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4fee\u6539\u6216\u6269\u5c55\u51fd\u6570\u884c\u4e3a\u7684\u4e00\u79cd\u9ad8\u7ea7\u6280\u672f\u3002\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u65b0\u51fd\u6570\u7684\u51fd\u6570\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<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Something is happening before the function is called.<\/strong><\/h2>\n<h2><strong>Hello!<\/strong><\/h2>\n<h2><strong>Something is happening after the function is called.<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u9012\u5f52\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u662f\u6307\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u5206\u6cbb\u95ee\u9898\u548c\u9012\u5f52\u5b9a\u4e49\u7684\u95ee\u9898\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>result = factorial(5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 120<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u662f\u63cf\u8ff0\u51fd\u6570\u529f\u80fd\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u7684<code>__doc__<\/code>\u5c5e\u6027\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;This function returns the sum of two numbers.&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>print(add.__doc__)  # \u8f93\u51fa: This function returns the sum of two numbers.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u51fd\u6570\u6ce8\u89e3<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u6ce8\u89e3\u7528\u4e8e\u4e3a\u51fd\u6570\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6dfb\u52a0\u5143\u6570\u636e\u3002\u867d\u7136Python\u4e0d\u5f3a\u5236\u6267\u884c\u6ce8\u89e3\uff0c\u4f46\u5b83\u4eec\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\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>result = add(3, 5)<\/p>\n<p>print(result)  # \u8f93\u51fa: 8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u9ad8\u9636\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9ad8\u9636\u51fd\u6570\u662f\u6307\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6216\u8fd4\u56de\u51fd\u6570\u7684\u51fd\u6570\u3002\u5e38\u89c1\u7684\u9ad8\u9636\u51fd\u6570\u6709<code>map<\/code>\u3001<code>filter<\/code>\u548c<code>reduce<\/code>\u3002<\/p>\n<\/p>\n<p><h4><code>map<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>map<\/code>\u51fd\u6570\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\u5230\u4e00\u4e2a\u6216\u591a\u4e2a\u5e8f\u5217\u4e0a\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4]<\/p>\n<p>squared_numbers = map(lambda x: x * x, numbers)<\/p>\n<p>print(list(squared_numbers))  # \u8f93\u51fa: [1, 4, 9, 16]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><code>filter<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>filter<\/code>\u51fd\u6570\u6839\u636e\u4e00\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5bf9\u4e00\u4e2a\u5e8f\u5217\u8fdb\u884c\u8fc7\u6ee4\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>even_numbers = filter(lambda x: x % 2 == 0, numbers)<\/p>\n<p>print(list(even_numbers))  # \u8f93\u51fa: [2, 4]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4><code>reduce<\/code>\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>reduce<\/code>\u51fd\u6570\u5bf9\u4e00\u4e2a\u5e8f\u5217\u8fdb\u884c\u7d2f\u8ba1\u8ba1\u7b97\uff0c\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u7684\u503c\u3002<code>reduce<\/code>\u51fd\u6570\u5728<code>functools<\/code>\u6a21\u5757\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import reduce<\/p>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<p>sum_of_numbers = reduce(lambda x, y: x + y, numbers)<\/p>\n<p>print(sum_of_numbers)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u751f\u6210\u5668\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u662f\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002\u751f\u6210\u5668\u51fd\u6570\u5728\u9700\u8981\u8fd4\u56de\u5927\u91cf\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u53ef\u4ee5\u9010\u6b65\u751f\u6210\u6570\u636e\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    a, b = 0, 1<\/p>\n<p>    for _ in range(n):<\/p>\n<p>        yield a<\/p>\n<p>        a, b = b, a + b<\/p>\n<p>fib = fibonacci(5)<\/p>\n<p>print(list(fib))  # \u8f93\u51fa: [0, 1, 1, 2, 3]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u5c40\u90e8\u53d8\u91cf\u548c\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5c40\u90e8\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ea\u80fd\u5728\u51fd\u6570\u5185\u90e8\u8bbf\u95ee\u3002\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h4>\u5c40\u90e8\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function():<\/p>\n<p>    x = 10  # x \u662f\u5c40\u90e8\u53d8\u91cf<\/p>\n<p>    print(x)<\/p>\n<p>my_function()  # \u8f93\u51fa: 10<\/p>\n<h2><strong>print(x)  # \u8fd9\u5c06\u5f15\u53d1 NameError\uff0c\u56e0\u4e3a x \u662f\u5c40\u90e8\u53d8\u91cf<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5168\u5c40\u53d8\u91cf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10  # x \u662f\u5168\u5c40\u53d8\u91cf<\/p>\n<p>def my_function():<\/p>\n<p>    global x<\/p>\n<p>    x = 20  # \u901a\u8fc7 global \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/p>\n<p>    print(x)<\/p>\n<p>my_function()  # \u8f93\u51fa: 20<\/p>\n<p>print(x)  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u547d\u540d\u7a7a\u95f4\u548c\u4f5c\u7528\u57df<\/h3>\n<\/p>\n<p><p>\u547d\u540d\u7a7a\u95f4\u662f\u4e00\u4e2a\u5305\u542b\u53d8\u91cf\u540d\u79f0\u5230\u5bf9\u8c61\u6620\u5c04\u7684\u5bb9\u5668\u3002Python\u6709\u56db\u79cd\u547d\u540d\u7a7a\u95f4\uff1a\u5c40\u90e8\u547d\u540d\u7a7a\u95f4\u3001\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u3001\u5185\u7f6e\u547d\u540d\u7a7a\u95f4\u548c\u5c01\u95ed\u547d\u540d\u7a7a\u95f4\u3002\u4f5c\u7528\u57df\u662f\u53d8\u91cf\u7684\u53ef\u8bbf\u95ee\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>LEGB\u89c4\u5219<\/h4>\n<\/p>\n<p><p>LEGB\u89c4\u5219\u63cf\u8ff0\u4e86Python\u67e5\u627e\u53d8\u91cf\u540d\u79f0\u7684\u987a\u5e8f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u90e8\u4f5c\u7528\u57df\uff08Local\uff09<\/strong>\uff1a\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u5c01\u95ed\u4f5c\u7528\u57df\uff08Enclosing\uff09<\/strong>\uff1a\u5d4c\u5957\u51fd\u6570\u7684\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u5168\u5c40\u4f5c\u7528\u57df\uff08Global\uff09<\/strong>\uff1a\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3002<\/li>\n<li><strong>\u5185\u7f6e\u4f5c\u7528\u57df\uff08Built-in\uff09<\/strong>\uff1aPython\u5185\u7f6e\u7684\u53d8\u91cf\u548c\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">x = &quot;global&quot;<\/p>\n<p>def outer():<\/p>\n<p>    x = &quot;enclosing&quot;<\/p>\n<p>    def inner():<\/p>\n<p>        x = &quot;local&quot;<\/p>\n<p>        print(x)  # \u8f93\u51fa: local<\/p>\n<p>    inner()<\/p>\n<p>    print(x)  # \u8f93\u51fa: enclosing<\/p>\n<p>outer()<\/p>\n<p>print(x)  # \u8f93\u51fa: global<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u516d\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u4e2d\u53ef\u80fd\u4f1a\u53d1\u751f\u5f02\u5e38\uff0c\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\u3002\u4f7f\u7528<code>try<\/code>\u3001<code>except<\/code>\u3001<code>else<\/code>\u548c<code>finally<\/code>\u5173\u952e\u5b57\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        print(&quot;Error: Division by zero is not allowed.&quot;)<\/p>\n<p>        return None<\/p>\n<p>    else:<\/p>\n<p>        return result<\/p>\n<p>    finally:<\/p>\n<p>        print(&quot;Execution of the divide function completed.&quot;)<\/p>\n<p>result = divide(10, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa: 5.0<\/p>\n<p>result = divide(10, 0)<\/p>\n<h2><strong>\u8f93\u51fa:<\/strong><\/h2>\n<h2><strong>Error: Division by zero is not allowed.<\/strong><\/h2>\n<h2><strong>Execution of the divide function completed.<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e03\u3001\u6587\u6863\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u7f16\u5199\u826f\u597d\u7684\u6587\u6863\u548c\u6d4b\u8bd5\u662f\u51fd\u6570\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u6587\u6863\u53ef\u4ee5\u901a\u8fc7\u6ce8\u91ca\u548c\u6587\u6863\u5b57\u7b26\u4e32\u7f16\u5199\uff0c\u800c\u6d4b\u8bd5\u53ef\u4ee5\u4f7f\u7528<code>unittest<\/code>\u6a21\u5757\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>\u7f16\u5199\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    Add two numbers.<\/p>\n<p>    Parameters:<\/p>\n<p>    a (int, float): The first number.<\/p>\n<p>    b (int, float): The second number.<\/p>\n<p>    Returns:<\/p>\n<p>    int, float: The sum of the two numbers.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>print(add.__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestAdd(unittest.TestCase):<\/p>\n<p>    def test_add_integers(self):<\/p>\n<p>        self.assertEqual(add(1, 2), 3)<\/p>\n<p>    def test_add_floats(self):<\/p>\n<p>        self.assertEqual(add(1.5, 2.5), 4.0)<\/p>\n<p>    def test_add_strings(self):<\/p>\n<p>        self.assertEqual(add(&quot;a&quot;, &quot;b&quot;), &quot;ab&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1aPython\u4e2d\u7684\u51fd\u6570\u662f\u4ee3\u7801\u590d\u7528\u548c\u7ec4\u7ec7\u7684\u57fa\u672c\u5355\u5143\u3002\u901a\u8fc7\u5b66\u4e60\u51fd\u6570\u7684\u5b9a\u4e49\u3001\u8c03\u7528\u3001\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u6587\u6863\u3001\u6d4b\u8bd5\u7b49\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7f16\u5199\u9ad8\u6548\u3001\u53ef\u8bfb\u548c\u53ef\u7ef4\u62a4\u4ee3\u7801\u7684\u80fd\u529b\u3002\u4e86\u89e3\u548c\u638c\u63e1\u51fd\u6570\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u95ed\u5305\u3001\u88c5\u9970\u5668\u548c\u9012\u5f52\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u7f16\u7a0b\u6280\u5de7\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u826f\u597d\u7684\u51fd\u6570\u8bbe\u8ba1\u548c\u7f16\u5199\u4e60\u60ef\u5c06\u6709\u52a9\u4e8e\u5f00\u53d1\u51fa\u9ad8\u8d28\u91cf\u7684\u8f6f\u4ef6\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\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u975e\u5e38\u7b80\u5355\u3002\u60a8\u53ea\u9700\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\uff0c\u540e\u63a5\u51fd\u6570\u540d\u548c\u62ec\u53f7\u3002\u51fd\u6570\u4f53\u7f29\u8fdb\u4e66\u5199\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">def greet(name):\n    return f&quot;Hello, {name}!&quot;\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>greet<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570<code>name<\/code>\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u95ee\u5019\u8bed\u3002<\/p>\n<p><strong>\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u5982\u4f55\u5728\u7a0b\u5e8f\u4e2d\u8c03\u7528\u5b83\uff1f<\/strong><br \/>\u5b9a\u4e49\u5b8c\u51fd\u6570\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u540d\u5e76\u4f20\u9012\u5fc5\u8981\u7684\u53c2\u6570\u6765\u8c03\u7528\u5b83\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">print(greet(&quot;Alice&quot;))\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa<code>Hello, Alice!<\/code>\u3002\u786e\u4fdd\u5728\u8c03\u7528\u65f6\u4f20\u9012\u6b63\u786e\u6570\u91cf\u548c\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u4ee5\u907f\u514d\u9519\u8bef\u3002<\/p>\n<p><strong>\u51fd\u6570\u53c2\u6570\u53ef\u4ee5\u6709\u54ea\u4e9b\u7c7b\u578b\uff1f<\/strong><br \/>Python\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u591a\u79cd\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5305\u62ec\u4f4d\u7f6e\u53c2\u6570\u3001\u5173\u952e\u5b57\u53c2\u6570\u3001\u9ed8\u8ba4\u53c2\u6570\u4ee5\u53ca\u53ef\u53d8\u53c2\u6570\uff08\u5982<code>*args<\/code>\u548c<code>**kwargs<\/code>\uff09\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">def example_func(a, b=10, *args, **kwargs):\n    print(a, b)\n    print(args)\n    print(kwargs)\n<\/code><\/pre>\n<p>\u6b64\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5fc5\u9700\u53c2\u6570<code>a<\/code>\uff0c\u4e00\u4e2a\u9ed8\u8ba4\u53c2\u6570<code>b<\/code>\uff0c\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u3002\u8fd9\u6837\u8bbe\u8ba1\u4f7f\u5f97\u51fd\u6570\u66f4\u52a0\u7075\u6d3b\uff0c\u53ef\u4ee5\u6ee1\u8db3\u591a\u79cd\u8c03\u7528\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5b9a\u4e49\u51fd\u6570\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u51fd\u6570\u6765\u4f7f\u7528\u3001\u51fd\u6570\u8c03\u7528\u7684\u57fa\u672c\u8bed\u6cd5\u662f\u51fd\u6570\u540d\u540e\u8ddf\u4e00\u5bf9\u5706\u62ec\u53f7 \u51fd\u6570\u662fPython\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1163485,"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\/1163482"}],"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=1163482"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163482\/revisions"}],"predecessor-version":[{"id":1163486,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1163482\/revisions\/1163486"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1163485"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1163482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1163482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1163482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}