擅长:python、mysql、java
<p>修改vars()返回的dict是<a href="http://docs.python.org/library/functions.html#vars" rel="nofollow noreferrer">not safe</a></p>
<blockquote>
<p>vars([object])¶</p>
<p>Without an argument, act like locals().</p>
<p>With a module, class or class instance object as argument (or
anything else that has a <strong>dict</strong>
attribute), return that attribute.</p>
<p>Note</p>
<p>The returned dictionary should not be modified: the effects on the
corresponding symbol table are
undefined.</p>
</blockquote>
<p>你的第二个例子是一个特例。<code>vars()</code>与全局命名空间中的<code>globals()</code>等效,并且<code>globals()</code>返回的dict的行为与您预期的一样(但是不受欢迎)</p>
<pre><code>>>> id(vars()),id(globals())
(3085426868L, 3085426868L)
</code></pre>