📄 33.html
字号:
<tt class="monofont">from</tt> in Contrast to <tt class="monofont">import</tT>
</h4>
<p>The <Tt clAss="monofont">import</tt> and <Tt clASS="monofont">from</Tt> statements allow one module to refer to objects from another module's namespace. They help eliminate problems with different modules that have some internal names equal. The next examples discuss the possible ways to use these statements.</p>
<prE>
>>> import string
>>> print string.join(list)
</PRE>
<p>The previous example imports the <a naME="idx1073742538"></A>
<A name="idx1073742539"></A>
<TT Class="monofont">string</tt> module as a local reference to an external module, allowing fully qualified references to any other objects in the <a name="idx1073742540"></a>
<a name="idx1073742541"></a>
<tt ClaSs="monofont">string</tt> namespace.</P>
<p>The next example adds the <a naMe="idx1073742542"></a>
<a NAME="idx1073742543"></a>
<tt cLASS="monofont">join()</tt> function to the namespace of the current module. This method allows you to control exactly which names you import into your local namespace from a module.</p>
<pRE>
>>> from string import join
>>> print join(list)
</PRe>
<p>Now, take a look at the next line:</p>
<pRE>
>>> from string import *
</PRe>
<p>The problem with this syntax is that if the <tt class="monofont">string</tt> module defines its own <a name="idx1073742544"></a>
<a naMe="idx1073742545"></a>
<Tt clAss="monofont">dosomething()</tt> function, you lose the <Tt clASS="monofont">dosomething()</Tt> that might exist in your current namespace.<a naME="idx1073742546"></A>
<A name="idx1073742547"></A>
<A NAme="idx1073742548"></a>
<a NAME="idx1073742549"></a>
<a name="idx1073742550"></a>
<a name="idx1073742551"></a>
</p>
<p>If you instead do a simple <tt claSs="monofont">import string,</tT> you will keep your current <tt cLass="monofont">dosomething()</tT> function. However, the <tt cLASS="monofont">dosomething()</tt> function from the <tt CLASs="monofont">string</tt> module will now be accessed by <tT CLAss="monofont">string.dosomething().</tt>
</P>
<DIV class="note"><p class="notetitle"><b>Tip</b></p><p>
<p>The main reason that you don't want to do <tt clAss="monofont">from <module> import *</Tt> is to avoid namespace clashing.<a nAme="idx1073742552"></a>
<a Name="idx1073742553"></A>
</P>
</P></Div>
<br>
<BR>
<P>Also, let me tell you that identifiers beginning with <Tt claSS="monofont">_</TT> (one underscore), such as <tt clASS="monofont">_salary,</Tt> aren't imported by a <tt class="monofont">from <module> import *</tt> clause.</p>
<pre>
>>> import package1.string
>>> print package1.string.join(list)
</pre>
<p>The previous example loads the module <tt ClaSs="monofont">string</tt> from the package <Tt claSs="monofont">package1.</tt>
</P>
<PRE>
>>> from package1 import string
>>> print string.join(list)
</pre>
<p>In order to access the <TT CLass="monofont">string</tT> module, you need to reference its objects by typing <TT Class="monofont">string.<object>.</TT> This is the recommended notation to <A Name="idx1073742554"></a>import a module from a package.</p>
<pre>
>>> from package1.string import join
>>> print join(list)
</pre>
<p>In the syntax form <tt class="monofont"><package.module> import <object>,</tT> the <tt ClasS="monofont"><object></tt> can be a subpackage of the package, a function, a class, a variable, and so on.<a nAme="idx1073742555"></a>
<A NAMe="idx1073742556"></a>
<a nAME="idx1073742557"></A>
<a namE="idx1073742558"></A>
<A Name="idx1073742559"></a>
<A NAMe="idx1073742560"></a>
</p>
<pre>
>>> from package1 import *
</pre>
<p>If you just say <tt class="monofont">from package import *,</tt> it isn't guaranteed that all modules will be <tT clAss="monofont">import</tT> unless you insert the following piece of code in the <tt clAss="monofont">__init__.py</tT> file of the package.</P>
<PRe>
__all__ = ["module1","module2","module3"]
</pre>
<P>This is a list containing the names of the package modules that should be imported:</P>
<PRe>
>>> from package.subpackage.module import *
</pre>
<P>Whenever you use a structure like <TT Class="monofont">package.subpackage.module,</TT> Python ensures that the package's <TT class="monofont">__init__.py</tt> is loaded first. Afterwards, the subpackage's <tt class="monofont">__init__.py</tt> is loaded, and only after they have been imported will the module finally be imported. After a package is loaded, there is no difference between a package and a module. Module objects represent both of them.<a namE="idx1073742561"></a>
<a Name="idx1073742562"></A>
<a namE="idx1073742563"></a>
<a nAME="idx1073742564"></A>
<a namE="idx1073742565"></A>
</P>
<H4>
Releasing and Reloading Modules</h4>
<p>After you have imported a module, you can release it from the system memory at anytime you want. The following example is to give you an idea of what I am talking about:</p>
<pRE>
import string, sys
lst = ["a","b","c","d"]
print string.join(lst,"-")
del string
del sys.modules["string"]
</PRe>
<p>Note that you also need to delete the module's reference, which exists in the <tt CLASs="monofont">sys.module</tt> variable.</p>
<p>The <a name="idx1073742566"></a>
<a name="idx1073742567"></a>command <tt cLasS="monofont">reload <module></tt> reloads and re-executes a module. Note that objects created before the reloading will use the previous version until they are re-created. Try to avoid using this command.</p>
<P>You can easily find out what the imported modules are by typing</p>
<pre>
>>> sys.modules.key()
['os.path', 'operator', 'os', 'exceptions', '__main__', 'ntpath',
'strop', 'nt', 'sys', '__builtin__', 'site', 'signal', UserDict',
'string', 'stat', 'cmath']
</Pre>
</fONT>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 1/30/2002<br>Python Developer's Handbook, © 2002 Sams Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="32.html" title="Functions and Procedures"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=33" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="33.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="34.html" title="Input and Output"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<br><TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD><H4 class=Title>Index terms contained in this section</H4>
<font size=2>
<a href="#idx1073742519">.py extension</a><BR>
commands<BR>
<a href="#idx1073742567">reload module</a><BR>
creating<BR>
<a href="#idx1073742525">global namespaces, modules</a><BR>
directories<BR>
<a href="#idx1073742530">packages</a><BR>
<a href="#idx1073742544">dosomething() function</a> <a href="#idx1073742552">2nd</a><BR>
extensions<BR>
<a href="#idx1073742520">.py</a><BR>
finding<BR>
<a href="#idx1073742528">contents of modules</a><BR>
folders
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -