子类python ContextVar

2024-06-12 12:13:49 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有合法的pythonic方法在ContextVar中存储一个附加属性

由于TypeError,我无法将ContextVar子类化

我能

    from contextvars import ContextVar as BaseContextVar

    class ContextVar:
        def __init__(self, name, *, default: Optional[Any], key: Optional[str]):
            self._var = BaseContextVar(name, default=default)
            self.key = key

但我不确定是否会有副作用

当然,我可以在映射中存储额外的值。但我不喜欢这样


Tags: 方法keynamefromimportselfdefault属性