擅长:python、mysql、java
<p>请参阅Python关于<a href="https://docs.python.org/3/faq/programming.html#how-do-i-share-global-variables-across-modules" rel="nofollow noreferrer">sharing global variables across modules</a>的文档:</p>
<blockquote>
<p>The canonical way to share information across modules within a single program is to create a special module (often called config or cfg).</p>
<p>config.py:
</p>
<pre><code>x = 0 # Default value of the 'x' configuration setting
</code></pre>
<p>Import the config module in all modules of your application; the module then becomes available as a global name.</p>
<p>main.py:
</p>
<pre><code>import config
print(config.x)
</code></pre>
<p>or
</p>
<pre><code>from config import x
print(x)
</code></pre>
</blockquote>
<hr/>
<blockquote>
<p>In general, <strong>don’t use</strong> from <em>modulename import *</em>. Doing so clutters the importer’s namespace, and makes it much harder for linters to detect undefined names.</p>
</blockquote>