Saturday, January 8, 2011

IDisposable+Using keyword in Python, another implementation using inheritance

class  IDisposableBaseClass: 

def  __enter__( self): 
print  "Entering..." 
return  self

def  __exit__( self,  type,  value,  traceback): 
self. Dispose() 
return  False

class  MyClass( IDisposableBaseClass): 

def  __init__( self): 
print  "Initializing..." 

def  Dispose( self): 
print  "Exiting..." 

def  Run( self): 
print  "Running" 

def  Main(): 

with  MyClass()  as  e: 
e. Run() 

Main()


No comments:

Post a Comment