{"id":1060457,"date":"2024-12-31T15:34:08","date_gmt":"2024-12-31T07:34:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1060457.html"},"modified":"2024-12-31T15:34:11","modified_gmt":"2024-12-31T07:34:11","slug":"python%e5%a6%82%e4%bd%95%e4%b8%a4%e4%b8%aa%e5%8c%85%e5%8f%98%e9%87%8f%e4%ba%92%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1060457.html","title":{"rendered":"python\u5982\u4f55\u4e24\u4e2a\u5305\u53d8\u91cf\u4e92\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4cdc43b0-a70c-426d-9e26-d6c030fe867d.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u4e24\u4e2a\u5305\u53d8\u91cf\u4e92\u7528\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e24\u4e2a\u5305\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\u6216\u8ba9\u5b83\u4eec\u4e92\u76f8\u8c03\u7528\u5bf9\u65b9\u7684\u53d8\u91cf\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c<strong>\u5982\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u3001\u5355\u4f8b\u6a21\u5f0f\u3001\u914d\u7f6e\u6587\u4ef6\u3001\u73af\u5883\u53d8\u91cf\u7b49<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u5e2e\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf\u662f\u6307\u5728\u6a21\u5757\u5185\u5b9a\u4e49\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u53ef\u4ee5\u5728\u6574\u4e2a\u6a21\u5757\u4e2d\u8bbf\u95ee\u3002\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u7684\u5bfc\u5165\uff0c\u4f7f\u5f97\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u8fd9\u4e9b\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6709\u4e24\u4e2a\u5305 <code>package1<\/code> \u548c <code>package2<\/code>\uff0c\u6211\u4eec\u5e0c\u671b\u5b83\u4eec\u80fd\u591f\u5171\u4eab\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># package1\/module1.py<\/p>\n<p>shared_variable = &quot;Hello from package1&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    from package2 import module2<\/p>\n<p>    print(module2.shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<h2><strong>package2\/module2.py<\/strong><\/h2>\n<p>shared_variable = &quot;Hello from package2&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    from package1 import module1<\/p>\n<p>    print(module1.shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 <code>module1.py<\/code> \u548c <code>module2.py<\/code> \u4e2d\u5206\u522b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a <code>shared_variable<\/code> \u7684\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4e14\u901a\u8fc7\u5728\u51fd\u6570\u5185\u90e8\u5bfc\u5165\u5bf9\u65b9\u7684\u6a21\u5757\u6765\u8bbf\u95ee\u5bf9\u65b9\u7684\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u6765\u5171\u4eab\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># singleton.py<\/p>\n<p>class Singleton:<\/p>\n<p>    _instance = None<\/p>\n<p>    shared_variable = None<\/p>\n<p>    def __new__(cls):<\/p>\n<p>        if cls._instance is None:<\/p>\n<p>            cls._instance = super(Singleton, cls).__new__(cls)<\/p>\n<p>        return cls._instance<\/p>\n<h2><strong>package1\/module1.py<\/strong><\/h2>\n<p>from singleton import Singleton<\/p>\n<p>singleton_instance = Singleton()<\/p>\n<p>singleton_instance.shared_variable = &quot;Hello from package1&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(Singleton().shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<h2><strong>package2\/module2.py<\/strong><\/h2>\n<p>from singleton import Singleton<\/p>\n<p>singleton_instance = Singleton()<\/p>\n<p>singleton_instance.shared_variable = &quot;Hello from package2&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(Singleton().shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>Singleton<\/code> \u7c7b\uff0c\u8be5\u7c7b\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u4f8b\u6765\u5171\u4eab\u53d8\u91cf <code>shared_variable<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u6765\u5b58\u50a8\u5171\u4eab\u53d8\u91cf\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u591a\u4e2a\u6a21\u5757\u4e4b\u95f4\u5171\u4eab\u914d\u7f6e\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>shared_variable = &quot;Initial value&quot;<\/p>\n<h2><strong>package1\/module1.py<\/strong><\/h2>\n<p>import config<\/p>\n<p>config.shared_variable = &quot;Hello from package1&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(config.shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<h2><strong>package2\/module2.py<\/strong><\/h2>\n<p>import config<\/p>\n<p>config.shared_variable = &quot;Hello from package2&quot;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(config.shared_variable)<\/p>\n<p>print_shared_variable()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5355\u72ec\u7684 <code>config.py<\/code> \u6587\u4ef6\u6765\u5b58\u50a8\u5171\u4eab\u53d8\u91cf <code>shared_variable<\/code>\uff0c\u5e76\u5728 <code>module1.py<\/code> \u548c <code>module2.py<\/code> \u4e2d\u5bfc\u5165\u548c\u4fee\u6539\u5b83\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u73af\u5883\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u5171\u4eab\u6570\u636e\uff0c\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># package1\/module1.py<\/p>\n<p>import os<\/p>\n<p>os.environ[&#39;SHARED_VARIABLE&#39;] = &#39;Hello from package1&#39;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(os.getenv(&#39;SHARED_VARIABLE&#39;))<\/p>\n<p>print_shared_variable()<\/p>\n<h2><strong>package2\/module2.py<\/strong><\/h2>\n<p>import os<\/p>\n<p>os.environ[&#39;SHARED_VARIABLE&#39;] = &#39;Hello from package2&#39;<\/p>\n<p>def print_shared_variable():<\/p>\n<p>    print(os.getenv(&#39;SHARED_VARIABLE&#39;))<\/p>\n<p>print_shared_variable()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>os.environ<\/code> \u6765\u8bbe\u7f6e\u548c\u83b7\u53d6\u73af\u5883\u53d8\u91cf <code>SHARED_VARIABLE<\/code>\uff0c\u5b9e\u73b0\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u53d8\u91cf\u5171\u4eab\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0Python\u4e2d\u4e24\u4e2a\u5305\u4e4b\u95f4\u53d8\u91cf\u7684\u4e92\u7528\u3002<strong>\u4f7f\u7528\u6a21\u5757\u7ea7\u522b\u7684\u53d8\u91cf<\/strong>\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u573a\u666f\uff1b<strong>\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u9700\u8981\u786e\u4fdd\u53d8\u91cf\u552f\u4e00\u6027\u7684\u573a\u666f\uff1b<strong>\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/strong>\uff0c\u9002\u7528\u4e8e\u591a\u4e2a\u6a21\u5757\u5171\u4eab\u914d\u7f6e\u6570\u636e\u7684\u573a\u666f\uff1b<strong>\u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/strong>\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u4e0d\u540c\u8fdb\u7a0b\u95f4\u5171\u4eab\u6570\u636e\u7684\u573a\u666f\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e24\u4e2a\u5305\u4e4b\u95f4\u7684\u53d8\u91cf\u5171\u4eab\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u4e24\u4e2a\u5305\u4e4b\u95f4\u7684\u53d8\u91cf\u5171\u4eab\u901a\u5e38\u6d89\u53ca\u5230\u6a21\u5757\u5bfc\u5165\u3002\u4f60\u53ef\u4ee5\u5728\u4e00\u4e2a\u5305\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u5728\u53e6\u4e00\u4e2a\u5305\u4e2d\u901a\u8fc7\u5bfc\u5165\u8be5\u5305\u6765\u8bbf\u95ee\u8fd9\u4e9b\u53d8\u91cf\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u6216<code>from ... import ...<\/code>\u8bed\u53e5\u6765\u5bfc\u5165\u53d8\u91cf\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u5728\u4e00\u4e2a\u5305\u4e2d\u4fee\u6539\u4e86\u53d8\u91cf\uff0c\u53e6\u4e00\u4e2a\u5305\u4f1a\u81ea\u52a8\u66f4\u65b0\u5417\uff1f<\/strong><br \/>\u5f53\u4f60\u5728\u4e00\u4e2a\u5305\u4e2d\u5bfc\u5165\u53e6\u4e00\u4e2a\u5305\u7684\u53d8\u91cf\u65f6\uff0c\u82e5\u76f4\u63a5\u5f15\u7528\u8be5\u53d8\u91cf\u7684\u503c\uff0c\u4fee\u6539\u7684\u7ed3\u679c\u4e0d\u4f1a\u81ea\u52a8\u53cd\u6620\u5728\u53e6\u4e00\u4e2a\u5305\u4e2d\u3002\u4e3a\u4e86\u786e\u4fdd\u53d8\u91cf\u7684\u66f4\u65b0\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u53ef\u53d8\u5bf9\u8c61\uff0c\u5982\u5217\u8868\u6216\u5b57\u5178\u3002\u5982\u679c\u5728\u4e00\u4e2a\u5305\u4e2d\u4fee\u6539\u4e86\u8fd9\u4e9b\u53ef\u53d8\u5bf9\u8c61\uff0c\u53e6\u4e00\u4e2a\u5305\u8bbf\u95ee\u7684\u5c06\u662f\u6700\u65b0\u7684\u72b6\u6001\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u786e\u4fdd\u4e24\u4e2a\u5305\u4e4b\u95f4\u7684\u53d8\u91cf\u4e92\u7528\uff1f<\/strong><br \/>\u786e\u4fdd\u4e24\u4e2a\u5305\u4e4b\u95f4\u7684\u53d8\u91cf\u4e92\u7528\u65f6\uff0c\u5efa\u8bae\u9075\u5faa\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a  <\/p>\n<ol>\n<li>\u4f7f\u7528\u660e\u786e\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u907f\u514d\u547d\u540d\u51b2\u7a81\u3002  <\/li>\n<li>\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u53d8\u91cf\u4f20\u9012\u5171\u4eab\u6570\u636e\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u3002  <\/li>\n<li>\u4f7f\u7528\u7c7b\u6216\u51fd\u6570\u6765\u5c01\u88c5\u53d8\u91cf\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u63a5\u53e3\u548c\u63a7\u5236\u8bbf\u95ee\u3002  <\/li>\n<li>\u8003\u8651\u4f7f\u7528\u5171\u4eab\u6570\u636e\u5e93\u6216\u7f13\u5b58\u673a\u5236\u6765\u7ba1\u7406\u548c\u540c\u6b65\u6570\u636e\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u5728\u4e0d\u540c\u7684Python\u5305\u4e4b\u95f4\u5171\u4eab\u548c\u7ba1\u7406\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u9700\u8981\u4e24\u4e2a\u5305\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\u6216\u8ba9\u5b83\u4eec\u4e92\u76f8\u8c03\u7528\u5bf9\u65b9\u7684\u53d8\u91cf\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u5982\u4f7f\u7528\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1060466,"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\/1060457"}],"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=1060457"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060457\/revisions"}],"predecessor-version":[{"id":1060470,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1060457\/revisions\/1060470"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1060466"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1060457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1060457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1060457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}