我正在为模块级构造创建一个进入/退出块。我有以下示例来测试如何从类中访问模块级变量:
_variableScope = ''
class VariableScope(object):
def __init__(self, scope):
self._scope = scope
def __enter__(self):
global _variableScope
_variableScope += self._scope
x = VariableScope('mytest')
x.__enter__()
print(_variableScope)
这让我得到了 的预期值'mytest',但是......
使用globalinside__enter__()方法是否正确和良好的做法?
相关分类