I'm trying to understand __builtins__ in Python (python3, specifically). (This thread is not about whether it's a good idea to clear the globals() dict. I know that's dumb.)
I used to think they were only accessed through the globals of the current context, but if that were true, the following would result in a NameError:
Obviously it doesn't. It appears that a reference to __builtins__ is squirrelled away somewhere magical and hidden at the beginning of the function's run. But how far does that extend?
If you look at this example, you can see that __builtins__ seems to be passed on to a completely different scope:
Likewise if you call it this way:
But not if you call it this way:
Okay, well that's absurd.
One last example indicates that it's squirrelled away using the value for the key "__builtins__" in the function's globals dict, but I have no idea as to how it decides whether it's passed on to other scopes:
I'd really enjoy an explanation. The documentation I found was exceedingly sparse on the subject of __builtins__.
I used to think they were only accessed through the globals of the current context, but if that were true, the following would result in a NameError:
Code:
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... globals().clear()
... print("hi")
...
>>> f()
hi
>>>
Obviously it doesn't. It appears that a reference to __builtins__ is squirrelled away somewhere magical and hidden at the beginning of the function's run. But how far does that extend?
If you look at this example, you can see that __builtins__ seems to be passed on to a completely different scope:
Code:
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... g_ = g
... globals().clear()
... print("hi from f")
... return g_
...
>>> def g(): print("hi from g")
...
>>> f()()
hi from f
hi from g
>>>
Likewise if you call it this way:
Code:
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... g_ = g
... globals().clear()
... print("hi from f")
... return g_
...
>>> def g(): print("hi from g")
...
>>> g = f() ; g()
hi from f
hi from g
>>>
But not if you call it this way:
Code:
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... g_ = g
... globals().clear()
... print("hi from f")
... return g_
...
>>> def g(): print("hi from g")
...
>>> g = f()
hi from f
>>> g()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in g
NameError: name 'print' is not defined
>>>
One last example indicates that it's squirrelled away using the value for the key "__builtins__" in the function's globals dict, but I have no idea as to how it decides whether it's passed on to other scopes:
Code:
Python 3.4.0 (default, Jun 19 2015, 14:18:46)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def mk_f():
... globs = {}
... bi = globs["__builtins__"] = type(__builtins__)("")
... bi.print = lambda *args: print("f says:", *args)
... def f():
... del globs["__builtins__"]
... print("bye")
... def g():
... print("hi from g")
... return g
... f = type(f)(f.__code__, globs, closure=f.__closure__)
... return f
...
>>> mk_f()()()
f says: bye
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 9, in g
NameError: name 'print' is not defined
>>>
I'd really enjoy an explanation. The documentation I found was exceedingly sparse on the subject of __builtins__.