https://www.geeksforgeeks.org/context-variables-in-python/
Context Variables in Python - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org
요즘 파이썬에서 parameter 로 contextvars 가 자주 나오길래 정리해봄

# import module
import contextvars
# declaring the variable
# to it's default value
cvar = contextvars.ContextVar("cvar",
default="default_val")
print("1. 디폴트값 출력 : \n",
cvar.get())
# calling set method
token = cvar.set("changed_1")
print("\n2. 가장 최근 set된 값 출력: \n",
cvar.get()) # changed_1
# set 메써드
token = cvar.set("changed_2")
print("\n3. 가장 최근 set된 값 출력: \n",
cvar.get()) # changed_2
print("\n4. return ContextVar , ContextVar 정보 출력 ( 이름, 디폴트값, id값 ): \n",
token.var)
print("\n5. set 하기 전에 set 된 값 출력: \n",
token.old_value)
# calling the reset method.
cvar.reset(token)
print("\n6. 가장 최근 바로 전 값 으로 reset 됨 : \n",
cvar.get())
'언어 정리 > python_lib,일급함수' 카테고리의 다른 글
unittest tutorial 개념 정리 (1) | 2022.09.19 |
---|---|
with 함수 ( __enter__, __exit__ ) (0) | 2022.09.19 |
functools.partial() 정리 (0) | 2022.08.17 |
PIL , 출력문 저장 lib(write,print,sys.out) (0) | 2022.08.04 |
pymysql ( + cursor , fetch, ) (0) | 2022.07.30 |
댓글