# 上下文变量-py3.7

Python 3.7引入了上下文变量的概念，上下文变量根据上下文会具有不同的值

> setval、printval两个不同的上下文
>
> 设置变量，在不同的上下文不同值：val=contextvars.ContextVar('var',default='0')

```python
>>> import contextvars
>>> import asyncio
>>> val=contextvars.ContextVar('var',default='0')
>>> async def setval():\
...   val.set('1')
... 
>>> async def printval():\
...   print(val.get())
... 
>>> asyncio.run(setval())
>>> asyncio.run(printval())
0
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/python/contextvar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
