Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The short version is: Assignment doesn't mutate anything in Python, it just rebinds names. Rebinding a different name, that happens to be currently bound to a reference to the same string as <code>sys.ps1</code> doesn't affect <code>sys.ps1</code> in any way.</p> <p>Let's go through step by step:</p> <pre><code>from sys import ps1 </code></pre> <p>This imports <code>sys</code> (but doesn't bind a name <code>sys</code> in the current globals), then binds the name <code>ps1</code> in the current globals to the same object as <code>sys</code>'s <code>ps1</code>.</p> <pre><code>ps1 = 'something else ' </code></pre> <p>This rebinds the name <code>ps1</code> in the current globals to the same object as the literal <code>'something else'</code>. There's no way that could possibly affect the <code>sys</code> module.</p> <pre><code>import sys </code></pre> <p>This imports <code>sys</code>, and then binds the name <code>sys</code> in the current globals to it.</p> <pre><code>sys.ps1 = 'something else ' </code></pre> <p>This looks up the name <code>sys</code> in the current globals, getting a reference to the <code>sys</code> module, then rebinds the name <code>ps1</code> in that module to the same object as the literal <code>'something else '</code>.</p> <hr> <p>Or, putting it in pseudo-Python terms, instead of in English…</p> <p>Your first code sample is like this:</p> <pre><code>tempspace._sys = __import__('sys') globals.ps1 = tempspace._sys.ps1 del tempspace._sys globals.ps1 = 'something else ' </code></pre> <p>Your second is like this:</p> <pre><code>globals.sys = sys_modules.sys = __import__('sys') globals.sys.ps1 = 'something else ' </code></pre> <hr> <p>Here is a transcript of what you described in the comments.</p> <pre><code>Python 2.7.2 (default, Jun 20 2012, 16:23:33) [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import sys &gt;&gt;&gt; from sys import ps1 &gt;&gt;&gt; id(sys.ps1) 4360831168 &gt;&gt;&gt; id(ps1) 4360831168 &gt;&gt;&gt; sys.ps1 = 'something else ' something else ps1 '&gt;&gt;&gt; ' something else </code></pre> <p><code>sys.ps1</code> and <code>ps1</code> have the same ID, because they are two different names that reference the same object.</p> <p>And changing <code>sys.ps1</code> does not change <code>ps1</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload