私はPythonインタプリタ(2.7.13)で次のように行っている:オブジェクトは "with"ブロック内でどのようにインスタンス化されますか?
>>> class test:
... def __init__(self):
... print "Object has been constructed!"
... def __enter__(self):
... print "Entering with"
... def __exit__(self, type, value, traceback):
... print "Exiting with"
...
>>>
>>> t1 = test()
Object has been constructed!
>>> print t1
<__main__.test instance at 0x7fed5383e3b0>
>>>
>>> with test() as t1:
... print "Inside 'with' block"
... print t1
...
Object has been constructed!
Entering with
Inside 'with' block
None
Exiting with
私の質問は:なぜwith
ブロック内print
出力none
していますか?オブジェクトはwith
によってインスタンス化されているようですが(少なくとも、コンストラクタへの呼び出しがあることがわかりますが)、名前空間内にt1が存在しないようです。それは普通ですか?
ありがとうございました、この明確な例の小道具! –