updates to context managers

This commit is contained in:
2016-01-15 18:15:24 -05:00
parent 600615021e
commit c843060d6d
+4 -4
View File
@@ -320,10 +320,10 @@ Context Managers
---------------- ----------------
A context manager is a Python object that provides extra contextual information A context manager is a Python object that provides extra contextual information
to an action. This extra information takes the form of running a function upon to an action. This extra information takes the form of running a callable upon
initiating the context using the ``with`` statement as well as running a function initiating the context using the ``with`` statement, as well as running a callable
upon completing all the code inside the ``with`` block. The most well known upon completing all the code inside the ``with`` block. The most well known
example of using a context manager is operating on a file: example of using a context manager is shown here, opening on a file:
.. code-block:: python .. code-block:: python
@@ -332,7 +332,7 @@ example of using a context manager is operating on a file:
Anyone familiar with this pattern knows that invoking ``open`` in this fashion Anyone familiar with this pattern knows that invoking ``open`` in this fashion
ensures that ``f``'s ``close`` method will be called at some point. This reduces ensures that ``f``'s ``close`` method will be called at some point. This reduces
a developer's cognitive load and makes code easier to read. a developer's cognitive load and makes the code easier to read.
There are two easy ways to implement this functionality yourself: using a class There are two easy ways to implement this functionality yourself: using a class
or using a generator. Let's implement the above functionality ourselves, starting or using a generator. Let's implement the above functionality ourselves, starting