python module name_浅谈python的module以及if __name__ == __main__的理解

模块化理解

A module is a file containing Python definitions and statements. (suffix .py is the file name and also the module name)

Within a module, the module name(as a string) is available as the value of the global variable name

if we only import the module name:

1

import sys

In this way, we can not use the fuctions defined in sys directly, but, we are allowed to access the functions by using module name:

1

2

sys.ppp()

sys.ooo()

we need module name before functions.

Additionaly, if we intend to use a function often, we can assign it to a local name:

1

2

ppp = sys.ppp

ppp()

More on Modules

Modules can import other modules.

The imported module names are placed in the importing module global symbol table.

1

from fibo import fib,fib2

On this condition, module name is not defined, it is not introduced. And we know the functions which we will use in the module.

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

1

from fibo import *

in most cases python programmers do not use this facility since it introduces an unknow set of names into the interpreter, possibly hiding some things you have already defined.

Additionally, it cause unreadable code.

executing modules as scripts

when we run a python module Within

1

python fibo.py

the code in the module will be executed, just as if you imported it, but with the mame set to “main“. By adding this code at the end of your module:

1

2

3

if __name__ == "__main__":

import sys

fib(int(sys.argv[1]))

you can make the file usable as a script as well as an importable module., because the code that parses the command line only runs if the module is executed as the main file:

1

2

python fibo.py 50

1 1 2 3 5 8

if the module is imported, the code is not run:

1

import fibo

this is often used either to provide a convenient user interface to a module, or for testing purposes(running the module as a script executes a test suite)

进程入口

像c,c++都有一个 main函数 作为进程的入口,进程的运行会从main开始。

python则不同,它属于脚本语言,不像编译型语言那样先将进程编译成二进制再运行,而是动态的逐行解释运行。从脚本第一行开始运行,没有同统一的入口。

一个python源码文档除了可以被直接运行外,还可以作为模块(也就是库)被导入。不管是直接运行还是导入,最顶层的代码都会被运行(python用缩进来区分代码层次)。而事实上,有一部分代码我不希望被运行。

现在我有一个q.py文档

1

2

3

4

5

6

PI = 3.14

def main():

print("PI:",PI)

main()

还有一个test.py文档:

1

2

3

4

5

6

7

8

from q import PI

def cal(r):

return PI * ( r ** 2)

def main():

print("area:",cal(2))

main()

在test.py里我导入了q.py中的PI值,现在我运行test.py输出为:

1

2

PI: 3.14

area: 12.56

我不想输出PI啊,只需要area就好,因为在运行test.py时候,所有包含的文档都会被运行。这个时候,if __name__ == ‘__main__’ 就派上用场了。把q.py改一下:

1

2

3

4

5

6

7

PI = 3.14

def main():

print("PI:",PI)

if __name__ == "__main__":

main()

然后再运行test.py,结果为:

1

area: 12.56

终于达到了想要的结果。

if __name__ == ‘__main__’就相当于 python模拟的进程入口。 主要注意的是,python本身并没有规定这么写,这只是一种编码习惯。总的来说,由于模块之间相互引用,不同模块可能都有这样的定义。到底哪个入口进程被选中,这取决于 __name__ 的值

__name__

__name__是内置变量,用于表示当前模块的名字。同时还能反映一个包的结构。

如果模块被直接运行,则代码块被运行;如果模块是被导入的,则代码块不被运行。

The module search path

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then search for a file named spam.py in a list of directories given by the variable sys.path, sys.path is initialized from these locations.

The directory containing the input script (or the current directory when no file is specified)

PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH)

The installation-dependent default.

The dir() function

The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings.

dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module builtins.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值