{"id":1149029,"date":"2025-01-13T16:44:39","date_gmt":"2025-01-13T08:44:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149029.html"},"modified":"2025-01-13T16:44:41","modified_gmt":"2025-01-13T08:44:41","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%85%a8%e5%b1%80%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149029.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25175804\/444ddb71-ed7b-4e14-9fae-734287925583.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u5168\u5c40\u5217\u8868\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5168\u5c40\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5217\u8868\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u5c06\u5217\u8868\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u3001\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u5168\u5c40\u53d8\u91cf\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5168\u5c40\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5217\u8868\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u5c06\u5217\u8868\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u3001\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u5168\u5c40\u53d8\u91cf\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\uff1a\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5217\u8868\u5c31\u662f\u5168\u5c40\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u591a\u4e2a\u51fd\u6570\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u5b83\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u5168\u5c40\u5217\u8868<\/p>\n<p>my_list = []<\/p>\n<p>def add_element(element):<\/p>\n<p>    # \u5411\u5168\u5c40\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20<\/p>\n<p>    my_list.append(element)<\/p>\n<p>def print_list():<\/p>\n<p>    # \u6253\u5370\u5168\u5c40\u5217\u8868<\/p>\n<p>    print(my_list)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>add_element(10)<\/p>\n<p>add_element(20)<\/p>\n<p>print_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>my_list<\/code>\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5b83\u662f\u4e00\u4e2a\u5168\u5c40\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u5728<code>add_element<\/code>\u548c<code>print_list<\/code>\u51fd\u6570\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5168\u5c40\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6700\u7b80\u5355\u7684\u5b9a\u4e49\u5168\u5c40\u5217\u8868\u7684\u65b9\u6cd5\u5c31\u662f\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5b83\u3002\u4efb\u4f55\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u7684\u53d8\u91cf\uff0c\u5305\u62ec\u5217\u8868\uff0c\u90fd\u662f\u5168\u5c40\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5168\u5c40\u5217\u8868<\/p>\n<p>global_list = []<\/p>\n<p>def add_to_list(item):<\/p>\n<p>    global_list.append(item)<\/p>\n<p>def show_list():<\/p>\n<p>    print(global_list)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>add_to_list(&#39;apple&#39;)<\/p>\n<p>add_to_list(&#39;banana&#39;)<\/p>\n<p>show_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u5217\u8868<code>global_list<\/code>\uff0c\u7136\u540e\u5728<code>add_to_list<\/code>\u51fd\u6570\u4e2d\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u5728<code>show_list<\/code>\u51fd\u6570\u4e2d\u6253\u5370\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528global\u5173\u952e\u5b57<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u5217\u8868\uff0c\u4f60\u5fc5\u987b\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u58f0\u660e\u8be5\u53d8\u91cf\u4e3a\u5168\u5c40\u53d8\u91cf\u3002\u8fd9\u6837Python\u624d\u4f1a\u77e5\u9053\u4f60\u60f3\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u4e0d\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5168\u5c40\u5217\u8868<\/p>\n<p>global_list = []<\/p>\n<p>def add_to_list(item):<\/p>\n<p>    global global_list<\/p>\n<p>    global_list.append(item)<\/p>\n<p>def show_list():<\/p>\n<p>    print(global_list)<\/p>\n<h2><strong>\u8c03\u7528\u51fd\u6570<\/strong><\/h2>\n<p>add_to_list(&#39;apple&#39;)<\/p>\n<p>add_to_list(&#39;banana&#39;)<\/p>\n<p>show_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5728<code>add_to_list<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528\u4e86<code>global<\/code>\u5173\u952e\u5b57\u6765\u58f0\u660e<code>global_list<\/code>\u662f\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u4ece\u800c\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u5168\u5c40\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u6a21\u5757\u9700\u8981\u5171\u4eab\u4e00\u4e2a\u5168\u5c40\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u5c06\u5168\u5c40\u5217\u8868\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u8be5\u5168\u5c40\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># globals.py<\/p>\n<p>global_list = []<\/p>\n<h2><strong>adder.py<\/strong><\/h2>\n<p>from globals import global_list<\/p>\n<p>def add_to_list(item):<\/p>\n<p>    global_list.append(item)<\/p>\n<h2><strong>shower.py<\/strong><\/h2>\n<p>from globals import global_list<\/p>\n<p>def show_list():<\/p>\n<p>    print(global_list)<\/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 adder<\/p>\n<p>import shower<\/p>\n<p>adder.add_to_list(&#39;apple&#39;)<\/p>\n<p>adder.add_to_list(&#39;banana&#39;)<\/p>\n<p>shower.show_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u5168\u5c40\u5217\u8868\u5b9a\u4e49\u5728<code>globals.py<\/code>\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728<code>adder.py<\/code>\u548c<code>shower.py<\/code>\u6a21\u5757\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528\u8be5\u5168\u5c40\u5217\u8868\u3002\u5728<code>main.py<\/code>\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u4e86<code>adder<\/code>\u548c<code>shower<\/code>\u6a21\u5757\u7684\u51fd\u6570\u6765\u4fee\u6539\u548c\u6253\u5370\u5168\u5c40\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u9677\u9631<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5168\u5c40\u53d8\u91cf\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u4f1a\u5f15\u5165\u4e00\u4e9b\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\uff0c\u56e0\u4e3a\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u4ece\u800c\u5f15\u5165\u96be\u4ee5\u53d1\u73b0\u7684\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u4f7f\u7528\u53c2\u6570\u4f20\u9012\u548c\u8fd4\u56de\u503c\u6765\u4ee3\u66ff\u5168\u5c40\u53d8\u91cf\u662f\u66f4\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u7ba1\u7406\u5168\u5c40\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u9677\u9631\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u6765\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002\u901a\u8fc7\u5c06\u5168\u5c40\u72b6\u6001\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\uff0c\u4f60\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u72b6\u6001\u7684\u8bbf\u95ee\u548c\u4fee\u6539\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class GlobalState:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.global_list = []<\/p>\n<p>    def add_to_list(self, item):<\/p>\n<p>        self.global_list.append(item)<\/p>\n<p>    def show_list(self):<\/p>\n<p>        print(self.global_list)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u72b6\u6001\u5bf9\u8c61<\/strong><\/h2>\n<p>global_state = GlobalState()<\/p>\n<h2><strong>\u4f7f\u7528\u5168\u5c40\u72b6\u6001\u5bf9\u8c61<\/strong><\/h2>\n<p>global_state.add_to_list(&#39;apple&#39;)<\/p>\n<p>global_state.add_to_list(&#39;banana&#39;)<\/p>\n<p>global_state.show_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a<code>GlobalState<\/code>\u7c7b\u6765\u7ba1\u7406\u5168\u5c40\u72b6\u6001\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u5168\u5c40\u72b6\u6001\u5bf9\u8c61<code>global_state<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u5168\u5c40\u72b6\u6001\u7684\u8bbf\u95ee\u548c\u4fee\u6539\uff0c\u907f\u514d\u5168\u5c40\u53d8\u91cf\u5e26\u6765\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u7ba1\u7406\u5168\u5c40\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u7ba1\u7406\u5168\u5c40\u72b6\u6001\uff0c\u907f\u514d\u5168\u5c40\u53d8\u91cf\u7684\u9677\u9631\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5355\u4f8b\u6a21\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class SingletonMeta(type):<\/p>\n<p>    _instances = {}<\/p>\n<p>    def __call__(cls, *args, kwargs):<\/p>\n<p>        if cls not in cls._instances:<\/p>\n<p>            cls._instances[cls] = super().__call__(*args, kwargs)<\/p>\n<p>        return cls._instances[cls]<\/p>\n<p>class GlobalState(metaclass=SingletonMeta):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.global_list = []<\/p>\n<p>    def add_to_list(self, item):<\/p>\n<p>        self.global_list.append(item)<\/p>\n<p>    def show_list(self):<\/p>\n<p>        print(self.global_list)<\/p>\n<h2><strong>\u83b7\u53d6\u5355\u4f8b\u5b9e\u4f8b<\/strong><\/h2>\n<p>global_state = GlobalState()<\/p>\n<h2><strong>\u4f7f\u7528\u5168\u5c40\u72b6\u6001\u5bf9\u8c61<\/strong><\/h2>\n<p>global_state.add_to_list(&#39;apple&#39;)<\/p>\n<p>global_state.add_to_list(&#39;banana&#39;)<\/p>\n<p>global_state.show_list()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>SingletonMeta<\/code>\u5143\u7c7b\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff0c\u786e\u4fdd<code>GlobalState<\/code>\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7ba1\u7406\u5168\u5c40\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u662f\u4e00\u79cd\u7528\u4e8e\u7ba1\u7406\u8d44\u6e90\u7684\u673a\u5236\uff0c\u901a\u5e38\u7528\u4e8e\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\u8d44\u6e90\u7684\u7ba1\u7406\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6765\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from contextlib import contextmanager<\/p>\n<p>@contextmanager<\/p>\n<p>def global_state_manager():<\/p>\n<p>    global_state = []<\/p>\n<p>    try:<\/p>\n<p>        yield global_state<\/p>\n<p>    finally:<\/p>\n<p>        global_state.clear()<\/p>\n<p>with global_state_manager() as global_state:<\/p>\n<p>    global_state.append(&#39;apple&#39;)<\/p>\n<p>    global_state.append(&#39;banana&#39;)<\/p>\n<p>    print(global_state)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>contextlib.contextmanager<\/code>\u88c5\u9970\u5668\u521b\u5efa\u4e86\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<code>global_state_manager<\/code>\uff0c\u5e76\u5728\u4e0a\u4e0b\u6587\u4e2d\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u786e\u4fdd\u5168\u5c40\u72b6\u6001\u5728\u4e0a\u4e0b\u6587\u7ed3\u675f\u65f6\u88ab\u6b63\u786e\u6e05\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9a\u4e49\u548c\u7ba1\u7406\u5168\u5c40\u5217\u8868\uff0c\u5305\u62ec\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5217\u8868\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u3001\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u5168\u5c40\u53d8\u91cf\u3001\u4f7f\u7528\u7c7b\u548c\u5bf9\u8c61\u3001\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u548c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1\u5168\u5c40\u53d8\u91cf\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\uff0c\u4f46\u5b83\u4eec\u4e5f\u4f1a\u5f15\u5165\u4e00\u4e9b\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5927\u578b\u9879\u76ee\u4e2d\u3002\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u548c\u8c03\u8bd5\uff0c\u56e0\u4e3a\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u4fee\u6539\u5168\u5c40\u53d8\u91cf\u7684\u503c\uff0c\u4ece\u800c\u5f15\u5165\u96be\u4ee5\u53d1\u73b0\u7684\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u5c3d\u91cf\u51cf\u5c11\u5168\u5c40\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u4f7f\u7528\u53c2\u6570\u4f20\u9012\u548c\u8fd4\u56de\u503c\u6765\u4ee3\u66ff\u5168\u5c40\u53d8\u91cf\u662f\u66f4\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u7ba1\u7406Python\u4e2d\u7684\u5168\u5c40\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u5168\u5c40\u5217\u8868\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u4e00\u4e2a\u5217\u8868\u6765\u521b\u5efa\u5168\u5c40\u5217\u8868\u3002\u53ea\u9700\u5728\u811a\u672c\u7684\u5f00\u5934\u6216\u5408\u9002\u7684\u4f4d\u7f6e\u5b9a\u4e49\u5217\u8868\u53d8\u91cf\uff0c\u4efb\u4f55\u51fd\u6570\u6216\u7c7b\u90fd\u53ef\u4ee5\u8bbf\u95ee\u548c\u4fee\u6539\u8be5\u5217\u8868\u3002\u4f8b\u5982\uff0c<code>global_list = []<\/code> \u4fbf\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5168\u5c40\u5217\u8868\u3002<\/p>\n<p><strong>\u5728\u51fd\u6570\u4e2d\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u5217\u8868\uff1f<\/strong><br \/>\u8981\u5728\u51fd\u6570\u4e2d\u8bbf\u95ee\u548c\u4fee\u6539\u5168\u5c40\u5217\u8868\uff0c\u9700\u8981\u4f7f\u7528<code>global<\/code>\u5173\u952e\u5b57\u3002\u9996\u5148\u5728\u51fd\u6570\u5185\u90e8\u58f0\u660e\u5217\u8868\u4e3a<code>global<\/code>\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u76f4\u63a5\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">global_list = []\n\ndef add_item(item):\n    global global_list\n    global_list.append(item)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u51fd\u6570<code>add_item<\/code>\u53ef\u4ee5\u5411\u5168\u5c40\u5217\u8868\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u4e0d\u540c\u6a21\u5757\u4e2d\u4f7f\u7528\u5168\u5c40\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u6a21\u5757\u7684\u65b9\u5f0f\u5728\u4e0d\u540c\u7684Python\u6587\u4ef6\u4e2d\u4f7f\u7528\u5168\u5c40\u5217\u8868\u3002\u9996\u5148\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u5b9a\u4e49\u5168\u5c40\u5217\u8868\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u6a21\u5757\u4e2d\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u8be5\u6a21\u5757\u5e76\u4f7f\u7528\u5217\u8868\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\"># module1.py\nglobal_list = []\n\n# module2.py\nfrom module1 import global_list\nglobal_list.append(&#39;new item&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5728\u591a\u4e2a\u6a21\u5757\u95f4\u5171\u4eab\u548c\u64cd\u4f5c\u5168\u5c40\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u5168\u5c40\u5217\u8868\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5728\u51fd\u6570\u5916\u90e8\u5b9a\u4e49\u5217\u8868\u3001\u4f7f\u7528global\u5173\u952e\u5b57\u5c06\u5217\u8868\u58f0\u660e\u4e3a\u5168\u5c40\u53d8\u91cf\u3001\u901a\u8fc7 [&hellip;]","protected":false},"author":3,"featured_media":1149037,"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\/1149029"}],"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=1149029"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149029\/revisions"}],"predecessor-version":[{"id":1149038,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149029\/revisions\/1149038"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149037"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}