Browse Source

Withable

pull/27/head
Jure Šorn 6 years ago
parent
commit
f74e5ccc7f
1 changed files with 4 additions and 4 deletions
  1. 8
      README.md

8
README.md

@ -766,12 +766,12 @@ class Counter:
### Withable
```python
class FileReader():
class MyOpen():
def __init__(self, filename):
self.filename = filename
def __enter__(self):
self.file = open(self.filename)
return self.file.read()
return self.file
def __exit__(self, *args):
self.file.close()
```
@ -779,8 +779,8 @@ class FileReader():
```python
>>> with open('test.txt', 'w') as file:
... file.write('Hello World!')
>>> with FileReader('test.txt') as text:
... print(text)
>>> with MyOpen('test.txt') as file:
... print(file.read())
Hello World!
```

Loading…
Cancel
Save