相关疑难解决方法(0)

使用上下文管理器时,对象变为无

为什么这不起作用:

class X:
    var1 = 1
    def __enter__(self): pass
    def __exit__(self, type, value, traceback): pass

with X() as z:
    print z.var1
Run Code Online (Sandbox Code Playgroud)

我明白了:

print z.var1
AttributeError: 'NoneType' object has no attribute 'var1'
Run Code Online (Sandbox Code Playgroud)

python with-statement

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×1

with-statement ×1