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

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -