forked from HaoZhang95/Python24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic04.py
More file actions
82 lines (57 loc) · 2.12 KB
/
Copy pathbasic04.py
File metadata and controls
82 lines (57 loc) · 2.12 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
with .. as
一个类实现了__enter()__和__exit__两个魔法方法,就表示这个类有了context上下文管理器
一般with配合上下文管理器使用, 上下文管理器就是给自己的类定义**鞍前马后**地处理
with 返回的对象 as xxx:
doSth()
1- 获取返回的对象本身必须实现enter和exit魔法方法
2- 获取该返回对象的时候就会触发enter鞍前方法
3- 当with里面
的代码执行完成后悔自动触发该对象的exit善后工作
"""
from contextlib import contextmanager
class MyFile(object):
def __init__(self, filename, mode):
self.filename = filename
self.mode = mode
# 上下文管理器的上文
def __enter__(self):
print("entering")
self.file = open(self.filename, self.mode)
return self.file
# 上下文管理器的下文,用于处理善后清除工作
def __exit__(self, *args):
print("will exit...")
self.file.close()
"""
1- open("output.txt", "w") 如果成功,返回值给f
2- 当with中的代码结束的时候,会**自动**调用close方法,实现原理就是默认实现了上下文管理器
3- with后面不止可以写open文件类,还可以写自己实现上下文管理器的自定义类
"""
def test01():
with open("output.txt", "w") as f:
f.write("Hello World 1.0")
with MyFile("output1.txt", "w") as f:
f.write("Hello World 2.0")
"""
使用contextmanager的模式, 方法前面加上@contextmanager的修饰器
1- 方法的yield返回值之前的代码就相当于enter方法
2- yield之后的代码就类似exit方法
"""
@contextmanager
def my_open(path, mode):
f = open(path, mode)
yield f
f.close()
def test02():
with my_open("output2.txt", "w") as f:
f.write("Hello World 3.0")
def main():
test01()
test02()
# test03()
if __name__ == '__main__':
main()
# python中的函数都是从上到下执行的,即使写了main函数也一样,通常把main函数写在最后,不支持像js一样的预解析
# def test03():
# print("......")