forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.import.py
More file actions
58 lines (43 loc) · 1.08 KB
/
11.import.py
File metadata and controls
58 lines (43 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
@description:
@author:XuMing
"""
from __future__ import print_function
from __future__ import unicode_literals
# 模块
# Python会将所有 .py 结尾的文件认定为Python代码文件
# __name__ 属性
# 有时候我们想将一个 .py 文件既当作脚本,又能当作模块用,
# 这个时候可以使用 __name__ 这个属性。
PI = 3.14
def get_sum(lst):
"""
Sum the values in the list
:param lst:
:return:
"""
total = 0
for v in lst:
total = total + v
return total
def test():
l = [1, 2, 3]
assert (get_sum(l) == 6)
print("test pass.")
if __name__ == '__main__':
test()
# 上文保存为ex.py
# 其他导入方法
# 可以从模块中导入变ex量:
from ex import PI, get_sum
print(PI) # 3.14
print(get_sum([2, 3])) # 5
# 或者使用 * 导入所有变量,不提倡,可能覆盖一些已有的函数
# 删除文件:
import os
os.remove('ex2.py')
# 包
# 导入包要求:
# 文件夹 foo 在Python的搜索路径中
# __init__.py 表示 foo 是一个包,它可以是个空文件。