1

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
zer0uno
  • 7,521
  • 13
  • 57
  • 86

1 Answers1

8

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.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So it can be said that they are embedded in the Python interpreter, isn't it? – zer0uno Jul 13 '14 at 14:16
  • 1
    @antox Note that there is a `builtins` module (in python3. `__builtin__` in python2). This module is defined by the `bltinmodule.c` file. So, technically, they are part of an external module, however this module is treated differently from other modules because it is searched if a global cannot be found. – Bakuriu Jul 13 '14 at 14:21
  • Doing a recap, builtin functions are implemented in the bltinmodule.c file so when python interpreter is compiled they became part of it. But those builtin functions are also available (as extra) inside the builtins module (in python3) for the reason expressed above. All right? – zer0uno Jul 13 '14 at 14:33
  • @antox: Sorta, kinda, yeah. Python has, as part of the binary, a module object called `__builtin__` or `builtins`, depending on the Python version, that is used to satisfy name lookups if those are not found in the globals. Your code can import that module object to directly look up names on it. – Martijn Pieters Jul 13 '14 at 14:36