1

I tried the following code:

with x as 1:
    y = 1
with z as 1:
    w = 1

The compiler reported a SyntaxError: invalid syntax. What is the problem?

[EDITED:] What I was trying to do is:

import tensorflow as tf
with tf.variable_scope("model"):
    y = 1
with tf.variable_scope("model2"):
    z = 1

I want to change the namescope in tensorflow for sharing variables. I looked at the online tutorial but that doesn't help much.

4
  • 1
    The error is because this isn't valid syntax—the thing after as has to be an assignment target, and you can't assign to constants like 1. Commented Jun 26, 2018 at 23:59
  • 2
    That probably doesn't help you at all. But what you've written is basically nonsense. If you can explain what you wanted that line of code to do, and why you thought with x as 1: would do it, we could probably explain the right way to do it. Commented Jun 27, 2018 at 0:00
  • By the way, what version of Python are you using? Python 2.7 and 3.5+ definitely give a slightly more informative SyntaxError: can't assign to literal, and I think that's been true since around 2.6 and 3.2. Commented Jun 27, 2018 at 0:02
  • I am using Python 3.6.5. Commented Jun 27, 2018 at 1:28

2 Answers 2

5

Oversimplifying slightly, your code:

with x as 1:
    y = 1

… translates to something like this:

try:
    1 = x.__enter__()
    y = 1
finally:
    1.__exit__()

Writing 1 = x.__enter__() is obviously going to raise a SyntaxError: can't assign to literal, because it doesn't mean anything to assign a new value to the literal constant 1.

Doing the same thing in a with statement raises the same exception. (In old versions of Python (I think just 2.5?), the error message wasn't quite as useful, and it just said SyntaxError: invalid syntax, but the problem is the same.)


Depending on what was in x, getting past the SyntaxError may well just raise a new exception, AttributeError: __enter__. Only context managers can be used in a with statement. Roughly, these are things that know how to clean up after themselves, and where it's important to get them to clean up at the end of some block of code no matter what. Files are the prototypical example: they call self.close() when you exit the block, which makes sure you don't get OS errors from having hundreds of open files lying around, or fail to flush the last write, or other such problems.

For more information on with, see PEP 343, the proposal that originally added with to Python 2.5, or Understanding Python's "with" statement (from effbot).


So, the question here is: what were you trying to do? If you just want to assign the value 1 to the name x, you already know how to do that, because you did it with y in the very next line: just x = 1. If you were trying to do something different… well, there probably is a way to do it, but with may well be nowhere near the right answer.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer. I think adding this source would turn it into a great one (it sheds some light on your first paragraph).
@yakobyd Added. Although really, that article seems a lot more focused on why 2.5 adds with, and how to do similar things in 2.4, than on how it works and what to do with it. (Probably because the PEP covered the how pretty well, but not much else.)
1

You misunderstand Python's with statement. If you tell us what you want to do, we can help.

with context_obj as name:

creates a context (execution environment) with three characteristics:

  1. On entering the context, the object's __enter__ method (function) is called;
  2. The return value of that function is assigned to name;
  3. On leaving the context, the object's __exit__ method is called, regardless of how we leave the context.

Your attempt, with x as 1 fails on all characteristics. x has no __enter__ or __exit__ method; there is no return value; you could not assign that return value to the constant 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.