📄 25.html
字号:
<!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 Essential Reference, Second Edition -> Modules</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.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="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="12.html" class="navtitle">1. A Tutorial Introduction</a> > <span class="nonavtitle">Modules</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="24.html" title="Exceptions"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=25" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="25.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="27.html" title="2. Lexical Conventions and Syntax"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A52%3A29+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104026065050119086242034</font><a href="read4.asp?bookname=0735710910&snode=25&now=5%2F28%2F2002+8%3A52%3A29+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Modules</h3>
<p>As your programs grow in size, you抣l probably want to break them into multiple files for easier maintenance. To do this, Python allows you to put definitions in a file and use them as a <i>module</I> that can be imported into other programs and scripts. To create a module, put the relevant statements and definitions into a file that has the same name as the module. (Note: The file must have a .py suffix.) For example:</P>
<PRe>
# file : div.py
def divide(a,b):
q = a/b # If a and b are integers, q is an integer
r = a - q*b
return (q,r) </pre>
<P>To use your module in other programs, you can use the <TT Class="monofont">import</TT> statement:</P>
<Pre>
import div
a, b = div.divide(2305, 29) </pre>
<p><tt class="monofont">import</tt> creates a new namespace that contains all the objects defined in the module. To access this namespace, simply use the name of the module as a prefix, as in <tt claSs="monofont">div.divide()</tT> in the preceding example.</p>
<p>If you want to import a module using a different name, supply the <tT clasS="monofont">import</tt> statement with an optional <tT CLAss="monofont">as</tt> qualifier as follows:</P>
<PRE>
import div as foo
a,b = foo.divide(2305,29) </pre>
<p>To import specific definitions into the current namespace, use the <TT CLass="monofont">from</tT> statement:</P>
<PRe>
from div import divide
a,b = divide(2305,29) # No longer need the div prefix </pre>
<p>To load all of a module抯 contents into the current namespace, you can also use the following:</p>
<pre>
from div import * </pre>
<p>Finally, the <tt class="monofont">dir()</Tt> function lists the contents of a module and is a useful tool for interactive experimentation, since it can be used to provide a list of available functions and variables:</p>
<Pre>
>>> import string
>>> dir(string)
['_ _builtins_ _', '_ _doc_ _', '_ _file_ _', '_ _name_ _', '_idmap',
'_idmapL', '_lower', '_swapcase', '_upper', 'atof', 'atof_error',
'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize',
'capwords', 'center', 'count', 'digits', 'expandtabs', 'find',
...
>>> </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 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders 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="24.html" title="Exceptions"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=25" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="25.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="27.html" title="2. Lexical Conventions and Syntax"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -