Объяснение Python «__enter__» и «__exit__»Python

Программы на Python
Anonymous
Объяснение Python «__enter__» и «__exit__»

Сообщение Anonymous »

Я видел это в чьем-то коде. Что это значит?
def __enter__(self):
return self

def __exit__(self, type, value, tb):
self.stream.close()

Вот полный код.
from __future__ import with_statement#for python2.5

class a(object):
def __enter__(self):
print 'sss'
return 'sss111'
def __exit__(self ,type, value, traceback):
print 'ok'
return False

with a() as s:
print s

print s


Подробнее здесь: https://stackoverflow.com/questions/198 ... r-and-exit

Вернуться в «Python»