python - with statement work on class -


{class foo(object):     def __enter__ (self):         print("enter")     def __exit__(self,type,value,traceback):         print("exit")     def method(self):         print("method") foo() instant:     instant.method()} 

execute py file , console shows these message:

enter exit  instant.method() attributeerror: 'nonetype' object has no attribute 'method' 

unable find methods?

__enter__ should return self:

class foo(object):     def __enter__ (self):         print("enter")         return self     def __exit__(self,type,value,traceback):         print("exit")     def method(self):         print("method") foo() instant:     instant.method() 

yields

enter method exit 

if __enter__ not return self, returns none default. thus, instant assigned value none. why error message

'nonetype' object has no attribute 'method'

(my emphasis)


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -