{"id":1174212,"date":"2025-01-15T17:14:24","date_gmt":"2025-01-15T09:14:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174212.html"},"modified":"2025-01-15T17:14:24","modified_gmt":"2025-01-15T09:14:24","slug":"python%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8%e5%87%bd%e6%95%b0%e7%9a%84%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1174212.html","title":{"rendered":"python\u5982\u4f55\u8c03\u7528\u51fd\u6570\u7684\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26075758\/48a330f2-f751-4df4-8287-a9a25b5a69ba.webp\" alt=\"python\u5982\u4f55\u8c03\u7528\u51fd\u6570\u7684\u53d8\u91cf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\u7684\u53d8\u91cf\u3002<strong>\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u901a\u8fc7\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u95ed\u5305\u3001\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u7b49<\/strong>\u90fd\u662f\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u89e3\u91ca\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u51fd\u6570\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u51fd\u6570\u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u4e2d\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u4f20\u9012\u53c2\u6570\uff0c\u51fd\u6570\u53ef\u4ee5\u63a5\u6536\u5916\u90e8\u6570\u636e\u5e76\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u51fd\u6570\u53c2\u6570\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\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570 <code>add<\/code> \u63a5\u53d7\u4e24\u4e2a\u53c2\u6570 <code>a<\/code> \u548c <code>b<\/code>\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u5c06\u5177\u4f53\u7684\u503c <code>3<\/code> \u548c <code>5<\/code> \u4f20\u9012\u7ed9 <code>a<\/code> \u548c <code>b<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u8c03\u7528\u51fd\u6570\u53d8\u91cf\u7684\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528\u51fd\u6570\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u51fd\u6570\u53c2\u6570\u662f\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u901a\u8fc7\u51fd\u6570\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u5c06\u5916\u90e8\u53d8\u91cf\u7684\u503c\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u5185\u4f7f\u7528\u8fd9\u4e9b\u503c\u8fdb\u884c\u8ba1\u7b97\u6216\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f4d\u7f6e\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u4f4d\u7f6e\u53c2\u6570\u662f\u51fd\u6570\u5b9a\u4e49\u4e2d\u6700\u57fa\u672c\u7684\u5f62\u5f0f\u3002\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4f20\u9012\u7684\u53c2\u6570\u6309\u4f4d\u7f6e\u987a\u5e8f\u5bf9\u5e94\u5230\u51fd\u6570\u5b9a\u4e49\u4e2d\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def multiply(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>result = multiply(4, 6)<\/p>\n<p>print(result)  # \u8f93\u51fa: 24<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>4<\/code> \u548c <code>6<\/code> \u5206\u522b\u88ab\u4f20\u9012\u7ed9\u53c2\u6570 <code>x<\/code> \u548c <code>y<\/code>\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u8fdb\u884c\u4e58\u6cd5\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>2. \u5173\u952e\u5b57\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5173\u952e\u5b57\u53c2\u6570\u5141\u8bb8\u5728\u8c03\u7528\u51fd\u6570\u65f6\u663e\u5f0f\u6307\u5b9a\u53c2\u6570\u540d\u79f0\uff0c\u4ece\u800c\u4e0d\u53d7\u53c2\u6570\u987a\u5e8f\u7684\u9650\u5236\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet(name, message):<\/p>\n<p>    print(f&quot;{message}, {name}!&quot;)<\/p>\n<p>greet(name=&quot;Alice&quot;, message=&quot;Hello&quot;)<\/p>\n<p>greet(message=&quot;Goodbye&quot;, name=&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e24\u4e2a\u8c03\u7528\u4e2d\uff0c\u53c2\u6570 <code>name<\/code> \u548c <code>message<\/code> \u88ab\u663e\u5f0f\u6307\u5b9a\uff0c\u56e0\u6b64\u8c03\u7528\u987a\u5e8f\u53ef\u4ee5\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><h3>3. \u9ed8\u8ba4\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9ed8\u8ba4\u53c2\u6570\u662f\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u5728\u8c03\u7528\u51fd\u6570\u65f6\u6ca1\u6709\u63d0\u4f9b\u5bf9\u5e94\u7684\u53c2\u6570\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\u3002<\/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 (\u4f7f\u7528\u9ed8\u8ba4\u7684\u6307\u6570\u503c 2)<\/p>\n<p>print(power(3, 3))   # \u8f93\u51fa: 27 (\u63d0\u4f9b\u4e86\u65b0\u7684\u6307\u6570\u503c 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5982\u679c\u8c03\u7528 <code>power<\/code> \u51fd\u6570\u65f6\u6ca1\u6709\u63d0\u4f9b <code>exponent<\/code> \u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528 <code>2<\/code> \u4f5c\u4e3a\u6307\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>4. \u53ef\u53d8\u957f\u5ea6\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u53d8\u957f\u5ea6\u53c2\u6570\u5141\u8bb8\u51fd\u6570\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u8fd9\u5728\u9700\u8981\u5904\u7406\u4e0d\u5b9a\u6570\u91cf\u7684\u8f93\u5165\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>*args<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>*args<\/code> \u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u4f4d\u7f6e\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5143\u7ec4\u4f20\u9012\u7ed9\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sum_all(*args):<\/p>\n<p>    total = 0<\/p>\n<p>    for num in args:<\/p>\n<p>        total += num<\/p>\n<p>    return total<\/p>\n<p>print(sum_all(1, 2, 3, 4))  # \u8f93\u51fa: 10<\/p>\n<p>print(sum_all(5, 10))       # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>kwargs<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 <code>kwargs<\/code> \u53ef\u4ee5\u63a5\u6536\u4efb\u610f\u6570\u91cf\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5b57\u5178\u4f20\u9012\u7ed9\u51fd\u6570\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<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>kwargs<\/code> \u63a5\u6536\u6240\u6709\u4f20\u9012\u7684\u5173\u952e\u5b57\u53c2\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5b57\u5178\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u901a\u8fc7\u5168\u5c40\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u5168\u5c40\u53d8\u91cf\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u76f4\u63a5\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u65f6\u8981\u7279\u522b\u5c0f\u5fc3\uff0c\u907f\u514d\u610f\u5916\u4fee\u6539\u5bfc\u81f4\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9a\u4e49\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>def print_global():<\/p>\n<p>    print(x)<\/p>\n<p>print_global()  # \u8f93\u51fa: 10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53d8\u91cf <code>x<\/code> \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u51fd\u6570 <code>print_global<\/code> \u4e2d\u76f4\u63a5\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>2. \u4fee\u6539\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u9700\u8981\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y = 5<\/p>\n<p>def modify_global():<\/p>\n<p>    global y<\/p>\n<p>    y = 15<\/p>\n<p>modify_global()<\/p>\n<p>print(y)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>global y<\/code> \u58f0\u660e <code>y<\/code> \u662f\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5176\u503c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u95ed\u5305<\/h2>\n<\/p>\n<p><p>\u95ed\u5305\u662f\u6307\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u5b9a\u4e49\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u5185\u90e8\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\u3002\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e26\u6709\u72b6\u6001\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u95ed\u5305<\/h3>\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>closure = outer_function(10)<\/p>\n<p>print(closure(5))  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>inner_function<\/code> \u662f <code>outer_function<\/code> \u7684\u95ed\u5305\uff0c\u53ef\u4ee5\u8bbf\u95ee <code>outer_function<\/code> \u7684\u53d8\u91cf <code>x<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u95ed\u5305\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e26\u6709\u72b6\u6001\u7684\u51fd\u6570\uff0c\u4f8b\u5982\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def make_counter():<\/p>\n<p>    count = 0<\/p>\n<p>    def counter():<\/p>\n<p>        nonlocal count<\/p>\n<p>        count += 1<\/p>\n<p>        return count<\/p>\n<p>    return counter<\/p>\n<p>counter_a = make_counter()<\/p>\n<p>print(counter_a())  # \u8f93\u51fa: 1<\/p>\n<p>print(counter_a())  # \u8f93\u51fa: 2<\/p>\n<p>counter_b = make_counter()<\/p>\n<p>print(counter_b())  # \u8f93\u51fa: 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a <code>counter<\/code> \u51fd\u6570\u90fd\u6709\u81ea\u5df1\u7684 <code>count<\/code> \u53d8\u91cf\uff0c\u901a\u8fc7\u95ed\u5305\u5b9e\u73b0\u4e86\u72ec\u7acb\u7684\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u662f\u5b9a\u4e49\u5728\u7c7b\u4e2d\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4f7f\u7528\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u72b6\u6001\u7ba1\u7406\u548c\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9a\u4e49\u7c7b\u548c\u5b9e\u4f8b\u53d8\u91cf<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def display(self):<\/p>\n<p>        print(self.value)<\/p>\n<p>obj = MyClass(20)<\/p>\n<p>obj.display()  # \u8f93\u51fa: 20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>value<\/code> \u662f\u7c7b <code>MyClass<\/code> \u7684\u5b9e\u4f8b\u53d8\u91cf\uff0c\u53ef\u4ee5\u5728\u65b9\u6cd5 <code>display<\/code> \u4e2d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<p><h3>2. \u4fee\u6539\u5b9e\u4f8b\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4fee\u6539\u5b9e\u4f8b\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def increment(self, amount):<\/p>\n<p>        self.value += amount<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>obj.increment(5)<\/p>\n<p>print(obj.value)  # \u8f93\u51fa: 15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u65b9\u6cd5 <code>increment<\/code> \u4fee\u6539\u4e86\u5b9e\u4f8b\u53d8\u91cf <code>value<\/code> \u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f7f\u7528\u7c7b\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u7c7b\u53d8\u91cf\u662f\u5b9a\u4e49\u5728\u7c7b\u7ea7\u522b\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    class_variable = 0<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        MyClass.class_variable += 1<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;Value: {self.value}, Class Variable: {MyClass.class_variable}&quot;)<\/p>\n<p>obj1 = MyClass(10)<\/p>\n<p>obj2 = MyClass(20)<\/p>\n<p>obj1.display()  # \u8f93\u51fa: Value: 10, Class Variable: 2<\/p>\n<p>obj2.display()  # \u8f93\u51fa: Value: 20, Class Variable: 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>class_variable<\/code> \u662f\u7c7b\u53d8\u91cf\uff0c\u88ab\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u901a\u8fc7\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u4fee\u6539\u51fd\u6570\u6216\u65b9\u6cd5\u884c\u4e3a\u7684\u9ad8\u7ea7\u51fd\u6570\u3002\u88c5\u9970\u5668\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u539f\u59cb\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9a\u4e49\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;Something is happening before the function is called.&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(&quot;Something is happening after the function is called.&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def say_hello(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>say_hello(&quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_decorator<\/code> \u662f\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u6dfb\u52a0\u4e86\u5728\u51fd\u6570\u8c03\u7528\u524d\u540e\u6253\u5370\u6d88\u606f\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u88c5\u9970\u5668\u4f20\u9012\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u672c\u8eab\u4e5f\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def repeat(times):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            for _ in range(times):<\/p>\n<p>                func(*args, kwargs)<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p>@repeat(3)<\/p>\n<p>def greet(name):<\/p>\n<p>    print(f&quot;Hello, {name}!&quot;)<\/p>\n<p>greet(&quot;Bob&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u88c5\u9970\u5668 <code>repeat<\/code> \u63a5\u53d7\u4e00\u4e2a\u53c2\u6570 <code>times<\/code>\uff0c\u6307\u5b9a\u51fd\u6570 <code>greet<\/code> \u88ab\u8c03\u7528\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u901a\u8fc7\u6a21\u5757\u548c\u5305<\/h2>\n<\/p>\n<p><p>\u6a21\u5757\u548c\u5305\u662fPython\u4e2d\u7ec4\u7ec7\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5c06\u53d8\u91cf\u548c\u51fd\u6570\u653e\u5728\u4e0d\u540c\u7684\u6a21\u5757\u4e2d\uff0c\u5e76\u5728\u9700\u8981\u65f6\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u548c\u5bfc\u5165\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u4e00\u4e2a\u6a21\u5757\u662f\u4e00\u4e2a\u5305\u542bPython\u4ee3\u7801\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5305\u542b\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7 <code>import<\/code> \u8bed\u53e5\u5bfc\u5165\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_module.py<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<h2><strong>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/strong><\/h2>\n<p>import my_module<\/p>\n<p>result = my_module.add(3, 4)<\/p>\n<p>print(result)  # \u8f93\u51fa: 7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_module<\/code> \u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u5305\u542b\u4e00\u4e2a\u51fd\u6570 <code>add<\/code>\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u5305\u7ec4\u7ec7\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5305\u662f\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684\u76ee\u5f55\uff0c\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u9879\u76ee\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># my_package\/__init__.py<\/p>\n<h2><strong>(\u53ef\u4ee5\u4e3a\u7a7a)<\/strong><\/h2>\n<h2><strong>my_package\/module1.py<\/strong><\/h2>\n<p>def func1():<\/p>\n<p>    print(&quot;This is function 1&quot;)<\/p>\n<h2><strong>my_package\/module2.py<\/strong><\/h2>\n<p>def func2():<\/p>\n<p>    print(&quot;This is function 2&quot;)<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>from my_package import module1, module2<\/p>\n<p>module1.func1()  # \u8f93\u51fa: This is function 1<\/p>\n<p>module2.func2()  # \u8f93\u51fa: This is function 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>my_package<\/code> \u662f\u4e00\u4e2a\u5305\uff0c\u5305\u542b\u4e24\u4e2a\u6a21\u5757 <code>module1<\/code> \u548c <code>module2<\/code>\uff0c\u53ef\u4ee5\u5206\u522b\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u53d8\u91cf\u548c\u914d\u7f6e\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u8bfb\u53d6\u8fd9\u4e9b\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528JSON\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>JSON\u662f\u4e00\u79cd\u5e38\u7528\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684 <code>json<\/code> \u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.json<\/p>\n<p>{<\/p>\n<p>    &quot;host&quot;: &quot;localhost&quot;,<\/p>\n<p>    &quot;port&quot;: 8000<\/p>\n<p>}<\/p>\n<h2><strong>main.py<\/strong><\/h2>\n<p>import json<\/p>\n<p>with open(&#39;config.json&#39;, &#39;r&#39;) as f:<\/p>\n<p>    config = json.load(f)<\/p>\n<p>print(config[&#39;host&#39;])  # \u8f93\u51fa: localhost<\/p>\n<p>print(config[&#39;port&#39;])  # \u8f93\u51fa: 8000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6 <code>config.json<\/code> \u5305\u542b\u4e24\u4e2a\u914d\u7f6e\u9879 <code>host<\/code> \u548c <code>port<\/code>\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bfb\u53d6\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528YAML\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>YAML\u662f\u4e00\u79cd\u66f4\u4eba\u6027\u5316\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pyyaml<\/code> \u5e93\u8fdb\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-yaml\"># config.yaml<\/p>\n<p>database:<\/p>\n<p>  host: localhost<\/p>\n<p>  port: 5432<\/p>\n<p>  user: admin<\/p>\n<p>  password: secret<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>import yaml<\/p>\n<p>with open(&#39;config.yaml&#39;, &#39;r&#39;) as f:<\/p>\n<p>    config = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(f)<\/p>\n<p>print(config[&#39;database&#39;][&#39;host&#39;])  # \u8f93\u51fa: localhost<\/p>\n<p>print(config[&#39;database&#39;][&#39;port&#39;])  # \u8f93\u51fa: 5432<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6 <code>config.yaml<\/code> \u5305\u542b\u6570\u636e\u5e93\u76f8\u5173\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u8bfb\u53d6\u5e76\u4f7f\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516b\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf<\/h2>\n<\/p>\n<p><p>\u73af\u5883\u53d8\u91cf\u662f\u64cd\u4f5c\u7cfb\u7edf\u7ea7\u522b\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u7528\u4e8e\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u8fd0\u884c\u73af\u5883\u3002Python\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>1. \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\u4e0d\u540c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>Windows<\/h4>\n<\/p>\n<p><pre><code class=\"language-cmd\">set MY_VARIABLE=my_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Linux\/MacOS<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">export MY_VARIABLE=my_value<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bfb\u53d6\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>my_variable = os.getenv(&#39;MY_VARIABLE&#39;, &#39;default_value&#39;)<\/p>\n<p>print(my_variable)  # \u8f93\u51fa: my_value (\u5982\u679c\u73af\u5883\u53d8\u91cf\u5df2\u8bbe\u7f6e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>os.getenv<\/code> \u51fd\u6570\u8bfb\u53d6\u73af\u5883\u53d8\u91cf <code>MY_VARIABLE<\/code>\uff0c\u5982\u679c\u672a\u8bbe\u7f6e\uff0c\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c <code>default_value<\/code>\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e5d\u3001\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u547d\u4ee4\u884c\u53c2\u6570\u662f\u8fd0\u884c\u7a0b\u5e8f\u65f6\u4f20\u9012\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\u3002Python\u53ef\u4ee5\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b9a\u4e49\u548c\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import argparse<\/p>\n<p>parser = argparse.ArgumentParser(description=&#39;Example script.&#39;)<\/p>\n<p>parser.add_argument(&#39;--name&#39;, type=str, required=True, help=&#39;Your name&#39;)<\/p>\n<p>parser.add_argument(&#39;--age&#39;, type=int, help=&#39;Your age&#39;)<\/p>\n<p>args = parser.parse_args()<\/p>\n<p>print(f&quot;Name: {args.name}&quot;)<\/p>\n<p>print(f&quot;Age: {args.age}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>argparse<\/code> \u6a21\u5757\u5b9a\u4e49\u548c\u89e3\u6790\u547d\u4ee4\u884c\u53c2\u6570 <code>--name<\/code> \u548c <code>--age<\/code>\u3002<\/p>\n<\/p>\n<p><h3>2. \u8fd0\u884c\u811a\u672c<\/h3>\n<\/p>\n<p><pre><code class=\"language-bash\">python script.py --name Alice --age 30<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u811a\u672c\u65f6\u4f20\u9012\u7684\u547d\u4ee4\u884c\u53c2\u6570\u5c06\u88ab\u89e3\u6790\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u5341\u3001\u901a\u8fc7\u914d\u7f6e\u7ba1\u7406\u5de5\u5177<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u590d\u6742\u7684\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u6765\u7ba1\u7406\u548c\u4f20\u9012\u53d8\u91cf\u3002\u5e38\u89c1\u7684\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\u5305\u62ec <code>configparser<\/code> \u548c <code>dotenv<\/code>\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528configparser\u7ba1\u7406\u914d\u7f6e<\/h3>\n<\/p>\n<p><p><code>configparser<\/code> \u6a21\u5757\u53ef\u4ee5\u8bfb\u53d6 <code>.ini<\/code> \u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\"># config.ini<\/p>\n<p>[database]<\/p>\n<p>host = localhost<\/p>\n<p>port = 5432<\/p>\n<p>user = admin<\/p>\n<p>password = secret<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;config.ini&#39;)<\/p>\n<p>print(config[&#39;database&#39;][&#39;host&#39;])  # \u8f93\u51fa: localhost<\/p>\n<p>print(config[&#39;database&#39;][&#39;port&#39;])  # \u8f93\u51fa: 5432<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>configparser<\/code> \u8bfb\u53d6 <code>config.ini<\/code> \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u8bbf\u95ee\u6570\u636e\u5e93\u76f8\u5173\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>2. \u4f7f\u7528dotenv\u7ba1\u7406\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p><code>dotenv<\/code> \u5e93\u53ef\u4ee5\u8bfb\u53d6 <code>.env<\/code> \u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-env\"># .env<\/p>\n<p>HOST=localhost<\/p>\n<p>PORT=8000<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><pre><code class=\"language-python\">from dotenv import load_dotenv<\/p>\n<p>import os<\/p>\n<p>load_dotenv()<\/p>\n<p>host = os.getenv(&#39;HOST&#39;)<\/p>\n<p>port = os.getenv(&#39;PORT&#39;)<\/p>\n<p>print(f&quot;Host: {host}&quot;)<\/p>\n<p>print(f&quot;Port: {port}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528 <code>dotenv<\/code> \u8bfb\u53d6 <code>.env<\/code> \u6587\u4ef6\u4e2d\u7684\u73af\u5883\u53d8\u91cf\uff0c\u5e76\u5728\u8fd0\u884c\u65f6\u4f7f\u7528\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u591a\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u8c03\u7528\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u51fd\u6570\u53c2\u6570\u3001\u5168\u5c40\u53d8\u91cf\u3001\u95ed\u5305\u3001\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u3001\u88c5\u9970\u5668\u3001\u6a21\u5757\u548c\u5305\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u3001\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u8fd8\u662f\u914d\u7f6e\u7ba1\u7406\u5de5\u5177\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u5e0c\u671b\u8fd9\u4e9b\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u793a\u4f8b\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u901a\u5e38\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u4ec5\u5728\u51fd\u6570\u8fd0\u884c\u65f6\u5b58\u5728\u3002\u5982\u679c\u4f60\u60f3\u5728\u51fd\u6570\u5916\u90e8\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u5b83\u4eec\u4f5c\u4e3a\u8fd4\u56de\u503c\u8fd4\u56de\uff0c\u6216\u8005\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u3002\u4f46\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u5e76\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u964d\u4f4e\u3002\u6700\u597d\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u51fd\u6570\u7684\u8fd4\u56de\u503c\u6765\u83b7\u53d6\u6240\u9700\u7684\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u8fd4\u56de\u503c\u6765\u4f20\u9012\u51fd\u6570\u5185\u7684\u53d8\u91cf\uff1f<\/strong><br \/>\u4f60\u53ef\u4ee5\u5c06\u51fd\u6570\u5185\u90e8\u7684\u53d8\u91cf\u901a\u8fc7<code>return<\/code>\u8bed\u53e5\u8fd4\u56de\u7ed9\u8c03\u7528\u8005\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u8ba1\u7b97\u67d0\u4e2a\u6570\u7684\u5e73\u65b9\uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u3002\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u540e\uff0c\u53ef\u4ee5\u5c06\u8fd4\u56de\u503c\u8d4b\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4ece\u800c\u5728\u51fd\u6570\u5916\u90e8\u4f7f\u7528\u8fd9\u4e2a\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u6a21\u5757\u6027\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5728Python\u7684\u51fd\u6570\u4e2d\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8be5\u5982\u4f55\u505a\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u5728\u51fd\u6570\u5185\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e\u8be5\u53d8\u91cf\u4e3a\u5168\u5c40\u3002\u8fd9\u6837\uff0c\u4efb\u4f55\u5728\u51fd\u6570\u5185\u5bf9\u8be5\u53d8\u91cf\u7684\u4fee\u6539\u90fd\u4f1a\u5f71\u54cd\u5230\u5168\u5c40\u8303\u56f4\u5185\u7684\u53d8\u91cf\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u505a\u6cd5\u9700\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u526f\u4f5c\u7528\uff0c\u5efa\u8bae\u5728\u9700\u8981\u65f6\u624d\u4f7f\u7528\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8c03\u7528\u51fd\u6570\u7684\u53d8\u91cf\u3002\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u3001\u901a\u8fc7\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u95ed\u5305\u3001\u901a\u8fc7\u7c7b\u7684\u5b9e\u4f8b\u53d8\u91cf\u7b49\u90fd\u662f [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174212"}],"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=1174212"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1174212\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1174212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1174212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1174212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}