From 4794c577aef789fabc31c1130fde2b714e128799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 8 Sep 2019 16:31:31 +0200 Subject: [PATCH] Context manager --- README.md | 2 ++ index.html | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f83b93c..676bf74 100644 --- a/README.md +++ b/README.md @@ -1156,6 +1156,8 @@ class Counter: ``` ### Context Manager +* **Enter() should lock the resources and return an object.** +* **Exit() should release the resources.** ```python class MyOpen(): def __init__(self, filename): diff --git a/index.html b/index.html index 93840d0..3dacab2 100644 --- a/index.html +++ b/index.html @@ -1094,7 +1094,10 @@ Z = dataclasses.make_dataclass('Z', [>>> counter(), counter(), counter() (1, 2, 3) -

Context Manager

class MyOpen():
+

Context Manager

    +
  • Enter() should lock the resources and return an object.
  • +
  • Exit() should release the resources.
  • +
class MyOpen():
     def __init__(self, filename):
         self.filename = filename
     def __enter__(self):
@@ -1104,6 +1107,7 @@ Z = dataclasses.make_dataclass('Z', [
+
>>> with open('test.txt', 'w') as file:
 ...     file.write('Hello World!')
 >>> with MyOpen('test.txt') as file: