有没有一种方法可以检测正在运行的代码是否在python的上下文管理器中执行

时间:2019-11-13 02:00:51

标签: python python-3.6 contextmanager

如标题所述,有没有办法做这样的事情:

def call_back():
    if called inside context:
        print("running in context")
    else:
        print("called outside context")

这将导致:

with CTM() as context:
    call_back()
call_back()

>>> "running in context"
>>> "called outside context"

0 个答案:

没有答案