Skip to content

Commit fbf22ef

Browse files
committed
提交示例代码
1 parent 34e0252 commit fbf22ef

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

xuanyuanyulong/2020-08-24-python-with-statement/__init__.py

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
JUST
2+
DO
3+
PYTHON
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class testContextManager:
2+
def __enter__(self):
3+
print("进入运行时上下文,调用__enter__方法")
4+
5+
def __exit__(self, exc_type, exc_value, traceback):
6+
print("退出运行时上下文,调用__exit__方法")
7+
8+
9+
with testContextManager() as o:
10+
pass
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
with open("test.txt", "r", encoding="utf-8") as f:
2+
s = f.readlines()
3+
4+
5+
print(s)
6+
7+
# try:
8+
# f = open("test.txt", "a", encoding="utf-8")
9+
# s = f.readlines()
10+
# except:
11+
# print("出现异常")
12+
# finally:
13+
# f.close()

0 commit comments

Comments
 (0)