Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a21443d commit 8730e92Copy full SHA for 8730e92
README.md
@@ -758,6 +758,28 @@ class Counter:
758
return self.a
759
```
760
761
+### Withable
762
+```python
763
+class FileReader():
764
+ def __init__(self, filename):
765
+ self.filename = filename
766
+ def __enter__(self):
767
+ self.file = open(self.filename)
768
+ return self.file.read()
769
+ def __exit__(self, *args):
770
+ self.file.close()
771
+ print(f'FileReader closed {self.filename!r}')
772
+```
773
+
774
775
+>>> with open('test.txt', 'w') as file:
776
+... file.write('Hello World!')
777
+>>> with FileReader('test.txt') as text:
778
+... print(text)
779
+Hello World!
780
+FileReader closed 'test.txt'
781
782
783
### Copy
784
```python
785
from copy import copy, deepcopy
0 commit comments