From 3cf7f4431849bf4cf70332f08d2b67431e09dbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sun, 5 Jan 2020 21:00:20 +0100 Subject: [PATCH] Duck types --- README.md | 4 +++- index.html | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8834e91..7803a6f 100644 --- a/README.md +++ b/README.md @@ -1174,6 +1174,8 @@ class Counter: ### Context Manager * **Enter() should lock the resources and return an object.** * **Exit() should release the resources.** +* **Any exception that happens inside the with block is passed to the exit() method.** +* **If it wishes to suppress the exception it must return a true value.** ```python class MyOpen(): def __init__(self, filename): @@ -1181,7 +1183,7 @@ class MyOpen(): def __enter__(self): self.file = open(self.filename) return self.file - def __exit__(self, *args): + def __exit__(self, exc_type, exc_value, traceback): self.file.close() ``` diff --git a/index.html b/index.html index 25e29af..d2c9b24 100644 --- a/index.html +++ b/index.html @@ -1116,13 +1116,15 @@ Z = dataclasses.make_dataclass('Z', [

Context Manager

class MyOpen():
     def __init__(self, filename):
         self.filename = filename
     def __enter__(self):
         self.file = open(self.filename)
         return self.file
-    def __exit__(self, *args):
+    def __exit__(self, exc_type, exc_value, traceback):
         self.file.close()