当前位置:首页 > 公司荣誉 >

Python模块-Modules

Python模块-Modules 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 script. 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.

如果你关闭Python解释器(IDLE),然后再打开,那么以前变量的定义则会丢失。因此,如果你想要写长一点的程序,最好先在编辑器中编写Python程序,然后将文件作为输入导入到IDLE解释执行。这就是所谓的创建一个脚本。当你的程序变得越来越大,你可能想要将程序分开到几个文件中。你可能想要写一个便利的函数,能够在几个程序中重复利用。

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 module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode).

为了支持这种功能,Python可以把代码用文件存储,并作为一个脚本,或用来与解释器交互。这种文件就叫做模块。模块中定义可以被导出到另一个模块中或导入到main模块中。

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:

一个模块就是一个文件,这个模块包含了Python的定义和语句。文件的名字就是模块的名字加后缀.py。在模块内部,该模块的名字可以使用全局变量__name来表示。例如,使用你最喜欢的文本编辑器创建一个文件fibo.py,文件内容如下:

复制代码

# Fibonacci numbers module

def fib(n): # write Fibonacci series up to n

a, b = 0, 1

while b < n:

print(b, end=' ')

a, b = b, a+b

print()

def fib2(n): # return Fibonacci series up to n

result = []

a, b = 0, 1

while b < n:

result.append(b)

a, b = b, a+b

return result

复制代码

Now enter the Python interpreter and import this module with the following command:

打开Python IDLE,用下面的命令导入模块(一般Python是通过sys.path指定的路径搜索模块):

>>> import fibo

This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions:

这个导入命令,不会将该模块的所有函数的名字自己导入到当前的符号表中,它仅仅只是将模块的名字导入到当前的符号表中,使用模块的名字,你可以访问它的函数:

>>> fibo.fib(1000)

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

>>> fibo.fib2(100)

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

>>> fibo.__name__

'fibo'

If you intend to use a function often you can assign it to a local name:

你也可以将函数赋给本地变量:

>>> fib = fibo.fib

>>> fib(500)

1 1 2 3 5 8 13 21 34 55 89 144 233 377

6.1. More on Modules

A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. [1] (They are also run if the file is executed as a script.)

模块包含可执行语句和函数定义。这些语句用于初始化模块,他们仅仅在模块第一次导入时被执行。

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. 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, modname.itemname.

每一个模块有它自己的符号表,这个符号表可以当作是存储模块中所有函数定义的全局符号表。因此,模块的拥有者可以在模块中使用全局变量而不用担心和另一个用户的全局变量引发冲突。另一方面,如果你很清楚你的意图,你可以访问模块的函数,请使用modname.itemname

Modules can import other modules. It is customary but not required to place all import 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.

模块能够导入其他模块。一种约定俗成的做法是:将所有import语句放在模块的开头。被导入的模块名字会被放置在当前模块的全局符号表中。

There is a variant of the import statement that imports names from a module directly into the importing module’s symbol table. For example:

这里列举了几种不同形式的import语句,从一个模块中直接导入函数到当前模块的符号表中:

>>> from fibo import fib, fib2

>>> fib(500)

1 1 2 3 5 8 13 21 34 55 89 144 233 377

This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, fibo is not defined).

这种导入方式不会将模块名字fibo放入到当前模块的符号表中(例如,输入fibo是未定义的)。

There is even a variant to import all names that a module defines:

这是另一种导入形式:导入所有在模块中定义的名字:

>>> from fibo import *

>>> fib(500)

1 1 2 3 5 8 13 21 34 55 89 144 233 377

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:黄冈网站建设 http://huanggang.45qun.com

上一篇:[零基础学python]一些关于Python的事情

下一篇:最后一页

相关文章