We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0400e8e commit 61abb23Copy full SHA for 61abb23
Doc/library/contextvars.rst
@@ -101,6 +101,21 @@ Context Variables
101
the value of the variable to what it was before the corresponding
102
*set*.
103
104
+ The token supports :ref:`context manager protocol <context-managers>`
105
+ to restore the corresponding context variable value at the exit from
106
+ :keyword:`with` block::
107
+
108
+ var = ContextVar('var', default='default value')
109
110
+ with var.set('new value'):
111
+ assert var.get() == 'new value'
112
113
+ assert var.get() == 'default value'
114
115
+ .. versionadded:: next
116
117
+ Added support for usage as a context manager.
118
119
.. attribute:: Token.var
120
121
A read-only property. Points to the :class:`ContextVar` object
0 commit comments