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

📄 node8.html

📁 python的入门教程,讲得挺不错的,有一些实例
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN"><html><head><link rel="STYLESHEET" href="style.css" type='text/css' /><link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /><link rel='start' href='../index.html' title='Python Documentation Index' /><link rel="first" href="tut.html" title='Python Tutorial' /><link rel='contents' href='node2.html' title="Contents" /><link rel='last' href='about.html' title='About this document...' /><link rel='help' href='about.html' title='About this document...' /><link rel="next" href="node9.html" /><link rel="prev" href="node7.html" /><link rel="parent" href="tut.html" /><link rel="next" href="node9.html" /><meta name='aesop' content='information' /><title>6. 模块 Modules </title></head><body><DIV CLASS="navigation"><div id='top-navigation-panel' xml:id='top-navigation-panel'><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="5. 数据结构 Data Structures"  href="node7.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="7. 输入和输出 Input and"  href="node9.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node7.html">5. 数据结构 Data Structures</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node9.html">7. 输入和输出 Input and</A></div><hr /></div></DIV><!--End of Navigation Panel--><div class='online-navigation'><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a><UL CLASS="ChildLinks"><LI><A href="node8.html#SECTION008100000000000000000">6.1 深入模块 More on Modules</a><UL><LI><A href="node8.html#SECTION008110000000000000000">6.1.1  模块搜索路径 The Module Search Path</a><LI><A href="node8.html#SECTION008120000000000000000">6.1.2 “编译”Python文件 ``Compiled'' Python files</a></ul><LI><A href="node8.html#SECTION008200000000000000000">6.2 标准模块 Standard Modules</a><LI><A href="node8.html#SECTION008300000000000000000">6.3 <tt class="function">dir()</tt> 函数 <tt class="function">dir()</tt> Function</a><LI><A href="node8.html#SECTION008400000000000000000">6.4 包 Packages</a><UL><LI><A href="node8.html#SECTION008410000000000000000">6.4.1 以 * 方式加载包 Importing * From a Package</a><LI><A href="node8.html#SECTION008420000000000000000">6.4.2 内置包(Intra-package)参考 Intra-package References</a><LI><A href="node8.html#SECTION008430000000000000000">6.4.3 多重路径中的包 Packages in Multiple Directories</a></ul></ul><!--End of Table of Child-Links--></div><HR><H1><A NAME="SECTION008000000000000000000"></A><A NAME="modules"></A><BR>6. 模块 Modules </H1><P>If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
Therefore, if you want to write a somewhat longer program, you are
better off using a text editor to prepare the input for the interpreter
and running it with that file as input instead.  This is known as creating a
<em>script</em>.  As your program gets longer, you may want to split it
into several files for easier maintenance.  You may also want to use a
handy function that you've written in several programs without copying
its definition into each program.<P>如果你退出 Python 解释器重新进入,以前创建的一切定义(变量和函数)就全部丢失了。因此,如果你想写一些长久保存的程序,最好使用一个文本编辑器来编写程序,把保存好的文件输入解释器。我们称之为创建一个<em>脚本</em>。程序变得更长一些了,你可能为了方便维护而把它分离成几个文件。你也可能想要在几个程序中都使用一个常用的函数,但是不想把它的定义复制到每一个程序里。<P>To support this, Python has a way to put definitions in a file and use
them in a script or in an interactive instance of the interpreter.
Such a file is called a <em>module</em>; definitions from a module can be
<em>imported</em> into other modules or into the <em>main</em> module (the
collection of variables that you have access to in a script
executed at the top level
and in calculator mode).<P>为了满足这些需要,Python提供了一个方法可以从文件中获取定义,在脚本或者解释器的一个交互式实例中使用。这样的文件被称为<em>模块</em>;模块中的定义可以<em>导入</em>到另一个模块或主模块中(在脚本执行时可以调用的变量集位于最高级,并且处于计算器模式)<P>A module is a file containing Python definitions and statements.  The
file name is the module name with the suffix <span class="file">.py</span> appended.  Within
a module, the module's name (as a string) is available as the value of
the global variable <code>__name__</code>.  For instance, use your favorite text
editor to create a file called <span class="file">fibo.py</span> in the current directory
with the following contents:<P>模块是包 括Python 定义和声明的文件。文件名就是模块名加上 <span class="file">.py</span> 后缀。模块的模块名(做为一个字符串)可以由全局变量 <code>__name__</code> 得到。例如,你可以用自己惯用的文件编辑器在当前目录下创建一个叫 <span class="file">fibo.py</span> 的文件,录入如下内容:<P><div class="verbatim"><pre>
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b &lt; n:
        print b,
        a, b = b, a+b

def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b &lt; n:
        result.append(b)
        a, b = b, a+b
    return result</pre></div><P>Now enter the Python interpreter and import this module with the
following command:<P>现在进入Python解释器,用如下命令导入这个模块:<P><div class="verbatim"><pre>
&gt;&gt;&gt; import fibo</pre></div><P>This does not enter the names of the functions defined in <code>fibo</code>
directly in the current symbol table; it only enters the module name
<code>fibo</code> there.
Using the module name you can access the functions:<P>这样做不会直接把 <code>fibo</code>中的函数导入当前的语义表;它只是引入了模块名 <code>fibo</code>。你可以通过模块名按如下方式访问这个函数:<P><div class="verbatim"><pre>
&gt;&gt;&gt; fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
&gt;&gt;&gt; fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
&gt;&gt;&gt; fibo.__name__
'fibo'</pre></div><P>If you intend to use a function often you can assign it to a local name:<P>如果你想要直接调用函数,通常可以给它赋一个本地名称:<P><div class="verbatim"><pre>
&gt;&gt;&gt; fib = fibo.fib
&gt;&gt;&gt; fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377</pre></div><P><H1><A NAME="SECTION008100000000000000000"></A><A NAME="moreModules"></A><BR>6.1 深入模块 More on Modules </H1><P>A module can contain executable statements as well as function
definitions.
These statements are intended to initialize the module.
They are executed only the
<em>first</em> time the module is imported somewhere.<A NAME="tex2html7"  HREF="#foot981"><SUP>6.1</SUP></A><P>模块可以像函数定义一样包含执行语句。这些语句通常用于初始化模块。它们只在模块<em>第一次</em>导入时执行一次。<A NAME="tex2html8"  HREF="#foot983"><SUP>6.2</SUP></A><P>Each module has its own private symbol table, which is used as the
global symbol table by all functions defined in the module.
Thus, the author of a module can use global variables in the module
without worrying about accidental clashes with a user's global
variables.<P>对应于定义模块中所有函数的全局语义表,每一个模块有自己的私有语义表。因此,模块作者可以在模块中使用一些全局变量,不会因为与用户的全局变量冲突而引发错误。<P>On the other hand, if you know what you are doing you can touch a
module's global variables with the same notation used to refer to its
functions,
<code>modname.itemname</code>.<P>另一方面,如果你确定你需要这个,可以像引用模块中的函数一样获取模块中的全局变量,形如:<code>modname.itemname</code>。<P>Modules can import other modules.  It is customary but not required to
place all <tt class="keyword">import</tt> statements at the beginning of a module (or
script, for that matter).  The imported module names are placed in the
importing module's global symbol table.<P>模块可以导入(import)其它模块。习惯上所有的
<tt class="keyword">import</tt>语句都放在模块(或脚本,等等)的开头,但这并不是必须的。被导入的模块名入在本模块的全局语义表中。<P>There is a variant of the <tt class="keyword">import</tt> statement that imports
names from a module directly into the importing module's symbol
table.  For example:<P><tt class="keyword">import</tt> 语句的一个变体直接从被导入的模块中导入命名到本模块的语义表中。例如:<P><div class="verbatim"><pre>
&gt;&gt;&gt; from fibo import fib, fib2
&gt;&gt;&gt; fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377</pre></div><P>This does not introduce the module name from which the imports are taken
in the local symbol table (so in the example, <code>fibo</code> is not
defined).

⌨️ 快捷键说明

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