Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Each of these return a dictionary:</p> <ul> <li><code>globals()</code> <em>always</em> returns the dictionary of the <em>module</em> namespace</li> <li><code>locals()</code> <em>always</em> returns <em>a</em> dictionary of the <em>current</em> namespace</li> <li><code>vars()</code> returns <em>either a</em> dictionary of the current namespace (if called with no argument) or <em>the</em> dictionary of the argument.</li> </ul> <p><code>locals</code> and <code>vars</code> could use some more explanation. if <code>locals()</code> is called inside a function it constructs a dictionary of the function namespace as of that moment and returns it -- any further name assignments are <em>not</em> reflected in the returned dictionary, and any assignments <em>to</em> the dictionary are <em>not</em> reflected in the actual local namespace:</p> <pre><code>def test(): a = 1 b = 2 huh = locals() c = 3 print(huh) huh['d'] = 4 print(d) </code></pre> <p>gives us:</p> <pre><code>{'a': 1, 'b': 2} Traceback (most recent call last): File "test.py", line 30, in &lt;module&gt; test() File "test.py", line 26, in test print(d) NameError: global name 'd' is not defined </code></pre> <p>Two notes:</p> <ol> <li>This behavior is CPython specific -- other Pythons may allow the updates to make it back to the local namespace</li> <li>In CPython 2.x it is possible to make this work by putting an <code>exec "pass"</code> line in the function.</li> </ol> <p>If <code>locals()</code> is called <em>outside</em> a function it returns the actual dictionary that is the current namespace. Further changes to the namespace <em>are</em> reflected in the dictionary, and changes to the dictionary <em>are</em> reflected in the namespace:</p> <pre><code>class Test(object): a = 'one' b = 'two' huh = locals() c = 'three' huh['d'] = 'four' print huh </code></pre> <p>gives us:</p> <pre><code>{ 'a': 'one', 'b': 'two', 'c': 'three', 'd': 'four', 'huh': {...}, '__module__': '__main__', } </code></pre> <p>So far, everything I've said about <code>locals()</code> is also true for <code>vars()</code>... here's the difference: <code>vars()</code> accepts a single object as its argument, and if you give it an object it returns the <code>__dict__</code> of that object. If that object was <em>not</em> a function the <code>__dict__</code> returned is that object's namespace:</p> <pre><code>class Test(object): a = 'one' b = 'two' def frobber(self): print self.c t = Test() huh = vars(t) huh['c'] = 'three' t.frobber() </code></pre> <p>which gives us:</p> <pre><code>three </code></pre> <p>If the object <em>was</em> a function, you still get its <code>__dict__</code>, but unless you're doing fun and interesting stuff its probably not very useful:</p> <pre><code>def test(): a = 1 b = 2 print test.c huh = vars(test) # these two lines are the same as 'test.c = 3' huh['c'] = 3 test() </code></pre> <p>which gives us:</p> <pre><code>3 </code></pre>
 

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