Skip to content

Commit

Permalink
Update content src/site/notes/ProgrammingLanguages/python/context man…
Browse files Browse the repository at this point in the history
…ager.md
  • Loading branch information
1024daniel committed Sep 13, 2024
1 parent 8ac971a commit f8c0485
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/site/notes/ProgrammingLanguages/python/context manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,26 @@ https://stackoverflow.com/questions/1984325/explaining-pythons-enter-and-exit
> [!NOTE] With statement
> **with** statement only works with objects that support the context management protocol (i.e. they have `__enter__` and `__exit__` methods). A class which implement both methods is known as context manager class.
with语句可以支持context manager的类,也就是实现了`__enter__``__exit__`的类,通过在with block的开始的时候会调用`__enter__`方法来申请资源,在with block结束的时候调用`__exit__`来进行释放资源
with语句可以支持context manager的类,也就是实现了`__enter__``__exit__`的类,通过在with block的开始的时候会调用`__enter__`方法来申请资源,在with block结束的时候调用`__exit__`来进行释放资源

```py
class Log:
def __init__(self,filename):
self.filename=filename
self.fp=None
def logging(self,text):
self.fp.write(text+'\n')
def __enter__(self):
print("__enter__")
self.fp=open(self.filename,"a+")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("__exit__")
self.fp.close()

with Log(r"C:\Users\SharpEl\Desktop\myfile.txt") as logfile:
print("Main")
logfile.logging("Test1")
logfile.logging("Test2")

```

0 comments on commit f8c0485

Please sign in to comment.