with
ifadesinde oluşturulan değişkenlerin kapsamı with
blokunun dışındadır (bkz. Variable defined with with-statement available outside of with-block?). Aşağıdaki kodu çalıştırdığınızda Ama:Neden bir blokun sonunda __del__ denir?
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
çıkış Foo.__del__
(yukarıda # line 1
at) foo yazdırmadan önce çağrılır gösterir:
__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
Sorum şu, neden ise Foo.__del__
, burada adlandırılan olduğunu with
bildirimi yeni bir yürütme kapsamı oluşturmuyor mu? bağlantının __del__
yöntem ikinci with
blokta denir eğer my_curs1.connection
sonra (yukarıda # line 2
bakınız) hala açık neden
Olası kopyası [MySQLdb.Connection nesnesiyle birlikte kullanabilir miyim?] (Http://stackoverflow.com/questions/11751703/can-i-use-with-statement-with-mysqldb-connection-object) – tzaman
Chengcheng, ikinci sorunun cevabını görmek için @ tzaman'ın bağlantısını tıklayın ve bu bölümü sorunuzdan kaldırın. Tek bir soruyu bir soruya tutturmak, StackOverflow'un düzenli tutulmasına yardımcı olur ve insanların cevapları daha hızlı bulmasını sağlar. Teşekkür ederim! – CodeMouse92
@tzaman Bu soru 3 yaşında ve cevabı yanlış. – Air