Where builtin functions are implemented

强颜欢笑 提交于 2019-12-12 04:28:28

问题


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:


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.



来源:https://stackoverflow.com/questions/24723583/where-builtin-functions-are-implemented

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!