`global` 是类访问模块级变量的正确方法吗?

我正在为模块级构造创建一个进入/退出块。我有以下示例来测试如何从类中访问模块级变量:


_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__()方法是否正确和良好的做法?


一只斗牛犬
浏览 133回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python