227

Is there a way to see how built in functions work in python? I don't mean just how to use them, but also how were they built, what is the code behind sorted or enumerate etc...?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1073865
  • 2,383
  • 3
  • 15
  • 7

8 Answers8

211

Since Python is open source you can read the source code.

To find out what file a particular module or function is implemented in you can usually print the __file__ attribute. Alternatively, you may use the inspect module, see the section Retrieving Source Code in the documentation of inspect.

For built-in classes and methods this is not so straightforward since inspect.getfile and inspect.getsource will return a type error stating that the object is built-in. However, many of the built-in types can be found in the Objects sub-directory of the Python source trunk. For example, see here for the implementation of the enumerate class or here for the implementation of the list type.

Rob Rose
  • 1,806
  • 22
  • 41
Chris
  • 44,602
  • 16
  • 137
  • 156
  • Can you give an example with `enumerate`? – Benjamin Dec 22 '11 at 19:08
  • following the OP, how about the source code for "sorted"? of course, inspect.getsourcefile(sorted) doesn't work. – Quetzalcoatl Nov 01 '18 at 23:29
  • 4
    @Quetzalcoatl the source code for `sorted()` is in [/Python/bltinmodule.c](https://github.com/python/cpython/blob/d246a6766b9d8cc625112906299c4cb019944300/Python/bltinmodule.c#L2236-L2237) although it just calls `list.sort()` so the real source is in [/Objects/listobject.c](https://github.com/python/cpython/blob/e42b705188271da108de42b55d9344642170aa2b/Objects/listobject.c#L2151-L2152) – Boris Verkhovskiy Apr 24 '19 at 06:07
  • 3
    would have been helpful if you had given an example of how to use ```__file__``` – stackoverflowpro Aug 25 '20 at 08:59
  • 5
    As a note to self, and for the future googlers: the `open()` functions is defined in `Modules/_io/_iomodule.c` in Python 3 (and not among the other builtins). – peter.slizik Sep 10 '20 at 10:02
  • Where can I find the `nt` module? (Example reference: https://github.com/python/cpython/blob/abde52cd8e31830bfc06c5803221faae6172104a/Lib/ntpath.py#L518 ) – cowlinator Dec 09 '20 at 22:37
  • 1
    I tried input.__file__ but it says 'builtin_function_or_method' object has no attribute '_ _ file _ _'. :-\ – Jitin Nov 17 '21 at 13:51
47

Here is a cookbook answer to supplement @Chris' answer, CPython has moved to GitHub and the Mercurial repository will no longer be updated:

  1. Install Git if necessary.
  2. git clone https://github.com/python/cpython.git

  3. Code will checkout to a subdirectory called cpython -> cd cpython

  4. Let's say we are looking for the definition of print()...
  5. egrep --color=always -R 'print' | less -R
  6. Aha! See Python/bltinmodule.c -> builtin_print()

Enjoy.

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 7
    `bltinmodule`. Arrrrrrrrrgh. Why did they have to spell it so badly? I tried a quick filesystem search for `builtin` and came up with nothing! – Pod Jan 19 '21 at 23:35
43

enter image description here

I had to dig a little to find the source of the following Built-in Functions as the search would yield thousands of results. (Good luck searching for any of those to find where it's source is)

Anyway, all those functions are defined in bltinmodule.c Functions start with builtin_{functionname}

Built-in Source: https://github.com/python/cpython/blob/master/Python/bltinmodule.c

For Built-in Types: https://github.com/python/cpython/tree/master/Objects

Community
  • 1
  • 1
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • 1
    A list is an object/type, not a builtin function. You can find the implementation details for that in `listobject.c` https://github.com/python/cpython/tree/master/Objects – user1767754 Feb 20 '19 at 23:32
  • `dir` is not implemented in C so it's not in that file. – Boris Verkhovskiy Dec 12 '20 at 23:28
  • Looking for the implementation of [builtin `pow` in bltinmodule.c](https://github.com/python/cpython/blob/main/Python/bltinmodule.c#L1927), I only find an unhelpful `static PyObject * builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp, PyObject *mod) { return PyNumber_Power(base, exp, mod); }`. Is there an easy way to find where the actual algorithm implementation is hidden? – Stef Dec 28 '21 at 11:12
26

The iPython shell makes this easy: function? will give you the documentation. function?? shows also the code. BUT this only works for pure python functions.

Then you can always download the source code for the (c)Python.

If you're interested in pythonic implementations of core functionality have a look at PyPy source.

tback
  • 11,138
  • 7
  • 47
  • 71
  • 1
    PyPy uses RPython for most built-in stuff, which can be nearly as low-level as C to almost as high-level as Python. Usually it's in between. In either case it's statically typed, so it isn't really Python. –  Dec 22 '11 at 19:19
  • 2
    See an early project for viewing source code of a builtin function: https://github.com/punchagan/cinspect – Thomas Jul 23 '14 at 17:38
16

2 methods,

  1. You can check usage about snippet using help()
  2. you can check hidden code for those modules using inspect

1) inspect:

use inpsect module to explore code you want... NOTE: you can able to explore code only for modules (aka) packages you have imported

for eg:

  >>> import randint  
  >>> from inspect import getsource
  >>> getsource(randint) # here i am going to explore code for package called `randint`

2) help():

you can simply use help() command to get help about builtin functions as well its code.

for eg: if you want to see the code for str() , simply type - help(str)

it will return like this,

>>> help(str)
Help on class str in module __builtin__:

class str(basestring)
 |  str(object='') -> string
 |
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |
 |  Method resolution order:
 |      str
 |      basestring
 |      object
 |
 |  Methods defined here:
 |
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |
 |  __format__(...)
 |      S.__format__(format_spec) -> string
 |
 |      Return a formatted version of S as described by format_spec.
 |
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |
 |  __getattribute__(...)
-- More  --
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
10

Let's go straight to your question.

Finding the source code for built-in Python functions?

The source code is located at cpython/Python/bltinmodule.c

To find the source code in the GitHub repository go here. You can see that all in-built functions start with builtin_<name_of_function>, for instance, sorted() is implemented in builtin_sorted.

For your pleasure I'll post the implementation of sorted():

builtin_sorted(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
{
    PyObject *newlist, *v, *seq, *callable;

    /* Keyword arguments are passed through list.sort() which will check
       them. */
    if (!_PyArg_UnpackStack(args, nargs, "sorted", 1, 1, &seq))
        return NULL;

    newlist = PySequence_List(seq);
    if (newlist == NULL)
        return NULL;

    callable = _PyObject_GetAttrId(newlist, &PyId_sort);
    if (callable == NULL) {
        Py_DECREF(newlist);
        return NULL;
    }

    assert(nargs >= 1);
    v = _PyObject_FastCallKeywords(callable, args + 1, nargs - 1, kwnames);
    Py_DECREF(callable);
    if (v == NULL) {
        Py_DECREF(newlist);
        return NULL;
    }
    Py_DECREF(v);
    return newlist;
}

As you may have noticed, that's not Python code, but C code.

Guzman Ojero
  • 2,812
  • 1
  • 20
  • 20
9

Quite an unknown resource is the Python Developer Guide.

In a (somewhat) recent GH issue, a new chapter was added for to address the question you're asking: CPython Source Code Layout. If something should change, that resource will also get updated.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
8

As mentioned by @Jim, the file organization is described here. Reproduced for ease of discovery:

For Python modules, the typical layout is:

Lib/<module>.py
Modules/_<module>.c (if there’s also a C accelerator module)
Lib/test/test_<module>.py
Doc/library/<module>.rst

For extension-only modules, the typical layout is:

Modules/<module>module.c
Lib/test/test_<module>.py
Doc/library/<module>.rst

For builtin types, the typical layout is:

Objects/<builtin>object.c
Lib/test/test_<builtin>.py
Doc/library/stdtypes.rst

For builtin functions, the typical layout is:

Python/bltinmodule.c
Lib/test/test_builtin.py
Doc/library/functions.rst

Some exceptions:

builtin type int is at Objects/longobject.c
builtin type str is at Objects/unicodeobject.c
builtin module sys is at Python/sysmodule.c
builtin module marshal is at Python/marshal.c
Windows-only module winreg is at PC/winreg.c
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135