Schrödinger's variable: the __class__ cell magically appears if you're checking for its presence?
There's a surprise here: >>> class B: ... print(locals()) ... def foo(self): ... print(locals()) ... print(__class__ in locals().values()) ... {'__module__': '__main__', '__qualname__': 'B'} >>> B().foo() {'__class__': <class '__main__.B'>, 'self': <__main__.B object at 0x7fffe916b4a8>} True It seems like the mere mention of __class__ is explicitly checked by the parser? Otherwise we should get something like NameError: name '__class__' is not defined Indeed, if you modify to only check the key instead, i.e. check for '__class__' in locals(), then we only have self in scope as expected. How do
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.