Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python doesn't store values in variables, it assigns names to objects. The <a href="http://docs.python.org/2/library/functions.html#locals" rel="nofollow">locals()</a> function will return all the names in the current namespace (or more specifically, the current scope). Let's start up a new interpreter session and see what <code>locals()</code> will give us.</p> <pre><code>&gt;&gt;&gt; locals() {'__builtins__': &lt;module '__builtin__' (built-in)&gt;, '__name__': '__main__', '__doc__': None, '__package__': None} </code></pre> <p>The only names currently in the namespace are built in names that Python puts there at start up. Here a quick one-liner to show us only the names we've assigned:</p> <pre><code>&gt;&gt;&gt; {k:v for k,v, in locals().iteritems() if k[0] != '_'} {} </code></pre> <p>That's better. Don't worry about how that one-liner works, let's move on and create a class.</p> <pre><code>&gt;&gt;&gt; class C(object): greeting = "I'm the first class" </code></pre> <p>When we define a class, it's name in places in the current scope:</p> <pre><code>&gt;&gt;&gt; {k:v for k,v, in locals().iteritems() if k[0] != '_'} {'C': &lt;class '__main__.C'&gt;} </code></pre> <p>The part is Python's way of saying that there's an object that's too big to print out, but it's the class object we defined. Let's look at the memory address that our class object is stored at. We can use the <a href="http://docs.python.org/2/library/functions.html#id" rel="nofollow">id()</a> function to find out.</p> <pre><code>&gt;&gt;&gt; id(C) 18968856 </code></pre> <p>The number that <code>id()</code> returns is the memory location of the argument. If you run these commands yourself, you'll see a different number, but the number doesn't change during a single session.</p> <pre><code>&gt;&gt;&gt; id(C) 18968856 </code></pre> <p>Now let's create an instance.</p> <pre><code>&gt;&gt;&gt; c = C() &gt;&gt;&gt; c.greeting "I'm the first class" </code></pre> <p>Now when we look at <code>locals()</code>, we can see both our class object, and our instance object.</p> <pre><code>&gt;&gt;&gt; {k:v for k,v, in locals().iteritems() if k[0] != '_'} {'C': &lt;class '__main__.C'&gt;, 'c': &lt;__main__.C object at 0x011BDED0&gt;} </code></pre> <p>Every instance object has a special member <code>__class__</code> that is a reference to the class object that the instance is an instance of.</p> <pre><code>&gt;&gt;&gt; c.__class__ &lt;class '__main__.C'&gt; </code></pre> <p>If we call <code>id()</code> on that variable, we can see it's a reference to the class <code>C</code> we just defined:</p> <pre><code>&gt;&gt;&gt; id(c.__class__) 18968856 &gt;&gt;&gt; id(c.__class__) == id(C) True </code></pre> <p>Now let's delete the name <code>C</code> from out local namespace:</p> <pre><code>&gt;&gt;&gt; del C &gt;&gt;&gt; {k:v for k,v, in locals().iteritems() if k[0] != '_'} {'c': &lt;__main__.C object at 0x011BDED0&gt;} &gt;&gt;&gt; C Traceback (most recent call last): File "&lt;pyshell#16&gt;", line 1, in &lt;module&gt; C NameError: name 'C' is not defined </code></pre> <p>That's exactly what we expect. The name <code>C</code> is no longer assigned to anything. However, our instance still has a reference to the class object.</p> <pre><code>&gt;&gt;&gt; c.__class__ &lt;class '__main__.C'&gt; &gt;&gt;&gt; id(c.__class__) 18968856 </code></pre> <p>As you can see, the class still exists, you just can't refer to it through the name <code>C</code> in the local namespace.</p> <p>Let's create a second class with the name <code>C</code>.</p> <pre><code>&gt;&gt;&gt; class C(object): greeting = "I'm the second class" &gt;&gt;&gt; {k:v for k,v, in locals().iteritems() if k[0] != '_'} {'C': &lt;class '__main__.C'&gt;, 'c': &lt;__main__.C object at 0x011BDED0&gt;} </code></pre> <p>If we create an instance of the second class, it behaves like you noticed:</p> <pre><code>&gt;&gt;&gt; c2 = C() &gt;&gt;&gt; c2.greeting "I'm the second class" &gt;&gt;&gt; c.greeting "I'm the first class" </code></pre> <p>To see why, let's look at the <code>id</code> of this new class. We can see that the new class object is stored in a different location from our first one.</p> <pre><code>&gt;&gt;&gt; id(C) 19011568 &gt;&gt;&gt; id(C) == id(C.__class__) False </code></pre> <p>This is why the instances can still work properly: both class object still exists separately, and each instance holds a reference to its object.</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. 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