title: Understanding tf.name_scope
body: <p>I am trying to understand tf.name_scope. The documentation mentions the following:</p>
  
  <p>"This context manager pushes a name scope, which will make the name of all operations added within it have a prefix.</p>
  
  <p>For example, to define a new Python op called my_op:</p>
  
  <pre><code>def my_op(a, b, c, name=None):
    with tf.name_scope("MyOp") as scope:
      a = tf.convert_to_tensor(a, name="a")
      b = tf.convert_to_tensor(b, name="b")
      c = tf.convert_to_tensor(c, name="c")
      # Define some computation that uses `a`, `b`, and `c`.
      return foo_op(..., name=scope)
  </code></pre>
  
  <p>When executed, the Tensors a, b, c, will have names MyOp/a, MyOp/b, and MyOp/c."</p>
  
  <p>My understanding is that the with block does not introduce a new local scope in Python. Under normal situation, the tensor variable a will also refer to the local parameter a of function my_op.   How is the name prefixing with "MyOp/" implemented using Python context? In the source code link for tf.name_scope (<a href="https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/framework/ops.py#L6423-L6442" rel="nofollow noreferrer">https://github.com/tensorflow/tensorflow/blob/v2.2.0/tensorflow/python/framework/ops.py#L6423-L6442</a>) there is an invocation of </p>
  
  <pre><code>ctx = context.context()
  </code></pre>
  
  <p>but I could not find the semantics of context.context(). Most context manager discussion talk about <strong>enter</strong> and <strong>exit</strong>, but no mention of variable renaming with some prefix. Is this some introspective mechanism in Python that allows the manipulation of Python variable scopes? Many thanks for any insights.</p>