Объем переменных, созданных в операторе with
, находится за пределами блока with
(см .: Variable defined with with-statement available outside of with-block?). Но когда я запускаю следующий код:Почему __del__ вызывается в конце блока с блоком?
class Foo:
def __init__(self):
print "__int__() called."
def __del__(self):
print "__del__() called."
def __enter__(self):
print "__enter__() called."
return "returned_test_str"
def __exit__(self, exc, value, tb):
print "__exit__() called."
def close(self):
print "close() called."
def test(self):
print "test() called."
if __name__ == "__main__":
with Foo() as foo:
print "with block begin???"
print "with block end???"
print "foo:", foo # line 1
print "-------- Testing MySQLdb -----------------------"
with MySQLdb.Connect(host="xxxx", port=0, user="xxx", passwd="xxx", db="test") as my_curs2:
print "(1)my_curs2:", my_curs2
print "(1)my_curs2.connection:", my_curs2.connection
print "(2)my_curs2.connection:", my_curs2.connection
print "(2)my_curs2.connection.open:", my_curs2.connection.open # line 2
Выходные данные показывают, что Foo.__del__
вызывается перед печатью Foo (в # line 1
выше):
__int__() called.
__enter__() called.
with block begin???
with block end???
__exit__() called.
__del__() called.
foo: returned_test_str
-------- Testing MySQLdb -----------------------
(1)my_curs2: <MySQLdb.cursors.Cursor object at 0x7f16dc95b290>
(1)my_curs2.connection: <_mysql.connection open to 'xxx' at 2609870>
(2)my_curs2.connection: <_mysql.connection open to 'xxx' at 2609870>
(2)my_curs2.connection.open: 1
Мой вопрос, почему Foo.__del__
называют здесь, если Оператор with
не создает новую область выполнения?
Кроме того, если __del__
метод данного соединения называется во втором with
блоке, я не понимаю, почему my_curs1.connection
все еще открыт после (см # line 2
выше).
Возможный дубликат [Можно ли использовать с инструкцией с объектом MySQLdb.Connection?] (Http://stackoverflow.com/questions/11751703/can-i-use-with-statement-with-mysqldb-connection-object) – tzaman
Chengcheng, см. Ссылку @ tzaman для ответа на ваш второй вопрос и удалить эту часть из вашего вопроса. Сохранение одной проблемы в одном вопросе помогает сохранить StackOverflow в порядке и позволяет быстрее находить ответы. Спасибо! – CodeMouse92
@tzaman Этот вопрос составляет 3 года, и его ответ неверен. – Air