Where builtin functions are implemented

2019-09-17 18:11发布

I tried to look around but I couldn't find anything clear about this topic.

Are built-in functions implemented in a module that is automatically imported every time Python is launched? In the case which is the module?

Or are built-in functions just embedded functions inside the Python interpreter?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-17 18:51

For CPython, the built-in functions are (for the most part) implemented in the bltinmodule.c file.

The exceptions are mostly the types; things like str and dict and list have their own C files in the Objects directory of the C source; these are listed as a table in the bltinmodule source.

Technically speaking, this is treated as a separate module object by the implementation, but one that is automatically searched when the current global namespace does not contain a name. So when you use abs() in your code, and there is no abs object in the global namespace, the built-ins module is also searched for that name.

It is also exposed as the __builtin__ module (or builtins in Python 3) so you can access the built-in names even if you shadowed any in your code. Like the sys module, however, it is compiled into the Python binary, and is not available as a separate dynamically loaded file.

查看更多
登录 后发表回答