模块学习重点:
导入: 全部导入,部分导入
调用: 可调用对象
私有对象的配置: 不可调用对象的配置。
#模块练习
#1个python文件就是1个模块
#
#语法 import 文件名(不要.py)
#import 文件名 as 文件名缩写
#一般放在文件开始
#该模块被调用的时候,有一个_name_属性 ,可以返回该模块的名字
import test_module as m
#from test_module import *
print(m.__name__)
print(__name__)
#访问模块中的变量 语法 模块名.变量名
print(m.a)
print(m.b)
m.test()
m.test2()
test2() # 如果导入到是整个模块,内置函数只能作为模块的属性调用
print(m._c)
# 测试 代码 只希望在自己的主程序内用(私有),不希望被调用,可以做main 函数判断
#因此 测试代码的常见语法是
def test4():
print('this is test4...')
if __name__=='__main__':
test4()
#也可以只引入模块中的部分内容
#
#from m import test
#from m import * # 引入所有 ,_开头的文件除外
## 引入所有内容 2个文件中有重复函数的时候,会有冲突
#from m import test2 as new_test2
from test_module import test2
test2() # 如果导入模块的某个函数,函数可以直接使用
执行结果
我是test module
test_module
__main__
10
15
this is test...
this is test2...
this is test2...
1
this is test4...
this is test2...