# Что-то типа дескриптора файла with open('file','r') as myfile: for line in myfile: print line # Пользовательский контекстный менеджер class Trace: def message(self,x): print x def __enter__(self) print "__enter__" return self def __exit__(self,exc_type,exc_value,exc_td) if exc_type is None: print "Exit normally" else: print "Exception: ", exc_type return False # call 1 with Trace() as action: action.message(1) print "reached" # call 2 with Trace() as action: action.message(2) raise TypeError print "not reached"
Python Справочник v0.05 © 2007-2024 Igor Salnikov aka SunDoctor