Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence?
<p>There's a surprise here:</p>

<pre><code>&gt;&gt;&gt; class B:
...     print(locals())
...     def foo(self):
...         print(locals())
...         print(__class__ in locals().values())
...         
{'__module__': '__main__', '__qualname__': 'B'}
&gt;&gt;&gt; B().foo()
{'__class__': &lt;class '__main__.B'&gt;, 'self': &lt;__main__.B object at 0x7fffe916b4a8&gt;}
True
</code></pre>

<p>It seems like the mere mention of <code>__class__</code> is explicitly checked by the parser? Otherwise we should get something like</p>

<pre><code>NameError: name '__class__' is not defined
</code></pre>

<p>Indeed, if you modify to only check the key instead, i.e. check for <code>'__class__' in locals()</code>, then we only have <code>self</code> in scope as expected.  </p>

<p>How does it happen that this variable gets magically injected into scope?  My guess is this is something to do with <code>super</code> - but I didn't use <code>super</code>, so why does the compiler create an implicit closure reference here if it isn't needed?</p>