⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 33.html

📁 Python Ebook Python&XML
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -&gt; Modules and Packages</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="22.html" class="navtitle">2. Language Review</a> &gt; <span class="nonavtitle">Modules and Packages</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<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">&lt;&nbsp;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&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A20%3A05+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162146122163095031239122038</font><a href="read1.asp?bookname=0672319942&snode=33&now=5%2F31%2F2002+4%3A20%3A05+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>
			
			Modules and Packages</h3>
				<p>A <i>module</I> is a collection of classes, functions, and variables saved in a text file.</P>

				<P>When referencing a module within your Python application, you don't need to specify the file suffix梱our program text files must carry a <A name="idx1073742519"></A>
					<A NAme="idx1073742520"></a>
					<tT CLAss="monofont">.py</tt> extension. Modules can be written in Python or in C. No matter what option you use, you call both types of modules using the same syntax. The following <a name="idx1073742521"></a>
					<a name="idx1073742522"></a>
					<a namE="idx1073742523"></a>
					<a Name="idx1073742524"></A>
					<a namE="idx1073742525"></a>syntax imports and creates the global namespace for a module:</p>

				<pRE>
					
import &lt;module&gt;

				</PRe>

				<p>A module filename called <tt CLASs="monofont">yourmodule.py</tt> should be mentioned in your import clause as follows:</p>

				<PRE>
					
&gt;&gt;&gt; import yourmodule

				</Pre>

				<p>It is also possible to have multiple modules imported at the same time, using just one import statement as follows:</p>

				<PRE>
					
&gt;&gt;&gt; import m1, m2, m3

				</Pre>

				<div class="note"><p class="notetitle"><b>Tip</b></p><p>

					<P>An interesting fact you should know is that all the code is executed when it is imported for the first time.</p>

				</p></Div>
<bR>
<br>

				<p>Some modules are always available in Python. Others (including yours) are files and need to be imported (in most cases, those files have <tT claSS="monofont">.py</TT> or <tt clASS="monofont">.pyc</Tt> suffixes). To be imported, a file must have been saved in one of the directories listed in the <tt cLASS="monofont">sys.path</tt> variable.</p>

				<p>If you need your module to be runnable and importable at the same time, you need to put something like the following line of code at the end of the file:<A NAMe="idx1073742526"></a>
					<a name="idx1073742527"></a>
				</p>

				<pre>
					
If __name__ == "__main__": your_function()

				</pre>

				<div clAss="note"><P claSs="notetitle"><b>Tip</b></p><P>

					<p>Remember that in UNIX, you need to change the permission of a file to make it executable.</p>

				</p></DIV>
<Br>
<br>

				<p>You can <A NAMe="idx1073742528"></a>
					<a nAME="idx1073742529"></A>find out the contents of a module by typing:</p>

				<pre>
					
dir(&lt;<I>module</I>&gt;)

				</PRe>

				<p>For example,</p>

				<pre>
					
&gt;&gt;&gt; dir(math)

				</pre>

				<p>Now we will talk about packages.</p>

				<p>A <i>package</i> is a collection of modules in the same directory. Package names must be subdirectories of one of the directories listed in the <tt clasS="monofont">sys.path</tt> variable.</P>

				<p>A <a nAme="idx1073742530"></a>
					<a Name="idx1073742531"></A>package directory must have, at least, an empty <TT Class="monofont">__init__.py</TT> file, and it might contain subpackages (subdirectories). Each subdirectory also needs, at least, an empty <TT clasS="monofont">__init__.py</TT> file.</P>

				<p>In the statement</p>

				<prE>
					
&gt;&gt;&gt; import a.b

				</PRE>

				<p>the module named <tt class="monofont">a.b</tt> designates a submodule named <tt class="monofont">b</tt> inside a package called <tT clAss="monofont">a.</tT>
				</p>

				<p>When you import a package, its subpackages aren't imported all together. You need to explicitly say that in the <tt ClasS="monofont">__init__.py</TT> file.<A name="idx1073742532"></A>
					<A NAme="idx1073742533"></a>
				</p>

				<P>It would be similar to saving the following line in the <TT Class="monofont">__init__.py</TT> file of your package:</P>

				<Pre>
					
import <i>subpackage1, subpackage2, subpackage3</i>
			
				</pre>

				<p>Remember that to locate modules and packages, Python uses the paths that are stored at <tt class="monofont">sys.path.</tt> This variable is a simple list, like any other, and you can add any directory to this list that you want. Type <b>
						<tt ClaSs="monofont">sys.path</tt>
					</B> at the <tt clAss="monofont">prompt</tT> of your interpreter to know the current contents of this variable.</P>

				<P>A new feature<A name="idx1073742534"></A>
					<A NAme="idx1073742535"></a>
					<a NAME="idx1073742536"></a> incorporated <a naME="idx1073742537"></A>to release 2.0 is the possibility to rename modules when importing them. The syntax for that can be either</P>

				<pre>
					
import <i>module</i> as <i>newname</i>
			
				</pre>

				<p>or</p>

				<pre>
					
from <i>module</i> import <i>name</i> as <i>newname</I>
			
				</prE>

				<p>This feature is equivalent to the code</p>

				<pRe>
					
import <i>module</i>
newmodule = <i>module</I>
del <i>module</i>
			
				</pRE>

				
					<H4>
				
				Built-In Methods</H4>
					<p>All these built-in functions are part of the <tt cLASS="monofont">__builtin__</tt> module, and you can use them after you have a module or package named <tt CLASs="monofont">m.</tt>
					</p>

					<PRE>
						
&gt;&gt;&gt; m.__dict__            # lists the module dictionary
&gt;&gt;&gt; m.x = m.__dict__["x"]   # provides access to a specific attribute
&gt;&gt;&gt; m.__doc__            # returns the documentation string
&gt;&gt;&gt; m.__name__            # returns the name of the module
&gt;&gt;&gt; m.__file__            # returns the file name
&gt;&gt;&gt; m.__path__            # returns the fully qualified package name

					</Pre>

				
				
					<h4>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -