Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f74e5cc

Browse files
committed
Withable
1 parent 2c5f8b0 commit f74e5cc

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,21 +766,21 @@ class Counter:
766766

767767
### Withable
768768
```python
769-
class FileReader():
769+
class MyOpen():
770770
def __init__(self, filename):
771771
self.filename = filename
772772
def __enter__(self):
773773
self.file = open(self.filename)
774-
return self.file.read()
774+
return self.file
775775
def __exit__(self, *args):
776776
self.file.close()
777777
```
778778

779779
```python
780780
>>> with open('test.txt', 'w') as file:
781781
... file.write('Hello World!')
782-
>>> with FileReader('test.txt') as text:
783-
... print(text)
782+
>>> with MyOpen('test.txt') as file:
783+
... print(file.read())
784784
Hello World!
785785
```
786786

0 commit comments

Comments
 (0)