import tensorflow as tf
...
with tf.Session() as sess:
output = sess.run(result)
print(output)
print(output)
为什么最后一行有效?由于output
是在生成器内部声明的,因此不超出范围吗?
答案 0 :(得分:2)
在python with
中没有类似作用域的方法,这篇文章对此进行了更清晰的解释
Variable defined with with-statement available outside of with-block?
为了节省您一些时间
the context manager will be available outside the with statement and that is not implementation or version dependent. with statements do not create a new execution scope.