【发布时间】:2016-07-03 12:07:21
【问题描述】:
import numpy as np
import tensorflow as tf
class simpleTest(tf.test.TestCase):
def setUp(self):
self.X = np.random.random_sample(size = (2, 3, 2))
def test(self):
a = 4
x = tf.constant(self.X, dtype=tf.float32)
if a % 2 == 0:
y = 2*x
else:
y = 3*x
z = 4*y
with self.test_session():
print y.eval()
print z.eval()
if __name__ == "__main__":
tf.test.main()
这里y是tensorflow变量,定义在if else块内,为什么可以在块外使用?
【问题讨论】:
-
python 中的“块”仅是 function 块。所有其他语句不产生新的作用域。例如:
for i in range(5):pass print(i)有效,所以for循环的i泄漏到循环外。
标签: python variables if-statement scope tensorflow