访问上下文管理器中的变量

时间:2019-12-11 14:23:20

标签: python contextmanager

我想使用val picker = Builder.datePicker() picker.addOnPositiveButtonClickListener { selection: Long -> saveListPosition() accountDetailsPresenter.editDate(selection, transaction, product, "") } picker.show(childFragmentManager, picker.toString()) 函数访问上下文管理器定义之外的变量。

我有:

context.py

eval

main.py

class Context:
    def __init__(self, arg1):
        self.arg1 = arg1

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        print(eval(self.arg1))

脚本from context import Context a = 'outside context' with Context(arg1='a') as c: pass 应该已经访问了变量main.py,但是我遇到了以下错误。

a

是否仅通过将变量名传递给另一个模块中的函数或类来访问变量Traceback (most recent call last): File "c:/code/context-test/main.py", line 8, in <module> pass File "c:\code\context-test\context.py", line 9, in __exit__ print(eval(self.arg1)) File "<string>", line 1, in <module> NameError: name 'a' is not defined

提前谢谢!

编辑最终,我想做类似的事情,然后在退出时在上下文管理器中对a进行操作。

a

0 个答案:

没有答案