python - Problem using the with-statement -
why doesn`t work:
class x: var1 = 1 def __enter__(self): pass def __exit__(self, type, value, traceback): pass class y: x = x() y = y() y.x z: print z.var1
i get:
print z.var1 attributeerror: 'nonetype' object has no attribute 'var1'
change definition of x
to
class x(object): var1 = 1 def __enter__(self): return self def __exit__(self, type, value, traceback): pass
with
assigns return value of __enter__()
method name after as
. __enter__()
returned none
, assigned z
.
i changed class new-style class (which not critical make work).
Comments
Post a Comment