Dan Dan - 1 month ago 12
Python Question

with tf.Session() as session(): ^ SyntaxError: can't assign to function call

In previous code, I tried the below and it worked but it's not working now. Can anyone see the problem?

with tf.Session() as session():
session.run(init)


But now I am getting this error. From looking at other posts, it seems to be because other people are using brackets when they should be using parenthesis. But I'm not using brackets and I can't see what is wrong.

with tf.Session() as session():
^
SyntaxError: can't assign to function call

Answer
with tf.Session() as session():

is somewhat same as:

session() = tf.Session()

which produces the same error because brackets () after session makes Python think left-hand side is an expression which is syntactically and semantically incorrect, so you should remove that:

with tf.Session() as session:
    ...