Найти - Пользователи
Полная версия: недопонимаю With statement
Начало » Python для новичков » недопонимаю With statement
1
axe
читаю документацию:
If BLOCK raises an exception, the __exit__(type, value, traceback)() is called with the exception details
from __future__ import with_statement
class a(object):
def __enter__( self ):
return {"test":"OK"}
def __exit__( self, arg1, arg2, arg3 ):
print arg1, arg2, arg3
b = a()
with b:
1/0
и возникает обычный exception:
<type ‘exceptions.ZeroDivisionError’> integer division or modulo by zero <traceback object at 0x009EE058>
Traceback (most recent call last):
File “python.py”, line 10, in <module>
1/0
ZeroDivisionError: integer division or modulo by zero
Если делать
with b:
pass
то __exit__ вызывается с параметрами None, None, None

О каких исключениях идёт речь?
Александр Кошелев
axe
и возникает обычный exception:
А вы какой ожидали?
Вот ваш __exit__ напечатал:
<type 'exceptions.ZeroDivisionError'> integer division or modulo by zero <traceback object at 0x009EE058>
axe
понял, спасибо!
я ожидал, что Exception не возникнет
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB