File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -152,10 +152,22 @@ Functions and classes provided:
152152
153153.. function :: nullcontext(enter_result=None)
154154
155- Return a context manager that returns enter_result from ``__enter__ ``, but
155+ Return a context manager that returns * enter_result * from ``__enter__ ``, but
156156 otherwise does nothing. It is intended to be used as a stand-in for an
157157 optional context manager, for example::
158158
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+
159171 def process_file(file_or_path):
160172 if isinstance(file_or_path, str):
161173 # If string, open file
You can’t perform that action at this time.
0 commit comments