File tree Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Expand file tree Collapse file tree 1 file changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -152,10 +152,22 @@ Functions and classes provided:
152
152
153
153
.. function :: nullcontext(enter_result=None)
154
154
155
- Return a context manager that returns enter_result from ``__enter__ ``, but
155
+ Return a context manager that returns * enter_result * from ``__enter__ ``, but
156
156
otherwise does nothing. It is intended to be used as a stand-in for an
157
157
optional context manager, for example::
158
158
159
+ def myfunction(arg, ignore_exceptions=False):
160
+ if ignore_exceptions:
161
+ # Use suppress to ignore all exceptions.
162
+ cm = contextlib.suppress(Exception)
163
+ else:
164
+ # Do not ignore any exceptions, cm has no effect.
165
+ cm = contextlib.nullcontext()
166
+ with cm:
167
+ # Do something
168
+
169
+ An example using *enter_result *::
170
+
159
171
def process_file(file_or_path):
160
172
if isinstance(file_or_path, str):
161
173
# If string, open file
You can’t perform that action at this time.
0 commit comments