I am very curious about the classes that exist in the module builtins
that are not accessible directly. Such as
type(lambda: 0) # __name__='function' of __module__='builtins'
type((lambda: 0).__code__) # __name__='code' (aka. bytecode) of __module__='builtins'
None.__class__ # __name__='NoneType' of __module__='builtins'
And the matriarch:
str.__mro__[1] # # __name__='object' of `__module__` 'builtins'
traceback as passed to the __exit__
magic method of a context manager is the same:
def __exit__(self, exc_type: Exception, exc_value: str, exc_traceback: 'bultins.traceback'):
pass
(The module traceback
is a module and simply shares the name, ditto for tracemalloc.Traceback
). In the above the object name is a string, but this is a rare example of a "hidden" builtins class and typehinting because for a function
instances typing.Callable
does the job.
Q: what is the name for these "hidden" builtin classes?
I understand that the builtins are written in C in CPython. I had a quick gander at the CPython Github repo and I could not figure out why unlike tuple
they are "hidden". I use the word "classes" as they have the same magic methods etc. and work like other classes:
NoneType() == None # True as expected
bytecode = code(...) # bytecode...
fxn = function(bytecode)
Q: Is there a PEP reason why they are not in builtins?
If it is simply to stop namespace pollution, I'd have though they'd be underscored objects in the builtin module say or in some module... unless they are somewhere else
Q: Can they be imported directly by somewhere else?!