You can't normally read the source code of a built-in function the way you'd open a Python file—many built-ins are implemented in C inside the interpreter itself. What you can do is inspect their signatures and docstrings, locate the built-in's name and module, and then read the matching C source in the CPython repository or a locally built copy. The check is: inspect.signature(), help(), and doc all return real information rather than errors or None.
That distinction matters because "built-in" in Python covers several different things. len, print, and sum are C functions compiled into the interpreter. Functions in the math and itertools modules are also C-implemented. Meanwhile, os.path.join and json.dumps are ordinary Python functions that happen to ship with the standard library—and those you genuinely can read line by line. The workflow below separates the cases so you don't waste time looking for C code that doesn't exist, or miss Python source that's right there on disk.
What "built-in" actually means before you look for code
Python uses the word loosely. There are three overlapping categories, and each has a different checking method.
Interpreter built-ins live in the builtins module: len, range, sorted, open, isinstance. They have no .py file. type(len) is builtin_function_or_method, and len.module is builtins.
C extension functions come from compiled modules like _math, _collections, _socket. math.sqrt.module reports math, but the implementation sits in Modules/mathmodule.c in CPython's source tree.
Standard-library Python functions look identical to user code at the call site but are pure Python: functools.reduce (well, that one's C-accelerated), textwrap.fill, pathlib.Path.read_text. Here inspect.getsource() usually works directly.
The good news: Python gives you reliable tools to tell these apart in seconds, and the same tools work in reverse to point you toward the C file when the source isn't Python at all.
Inspecting the signature and docstring first
Start with what the object tells you about itself. This is the reversible, zero-risk check, and it answers most everyday questions like "what arguments does this take?" or "what does it return?"
Open an interactive session or a scratch script and run:
For sorted, inspect.signature returns (iterable, /, *, key=None, reverse=False). doc is a multi-line description. type() returns <class 'builtin_function_or_method'>, and module returns builtins.
A few caveats worth knowing before you trust the output. inspect.signature() raises ValueError for some C functions that don't expose argument metadata—older or hand-written C extensions are the common culprits. When that happens, help(target) often still produces a usable text signature because it falls back to parsing the docstring. And doc can be None for functions that carry no docstring at all, which tells you nothing about the implementation.
help(sorted) prints the signature, the docstring, and, for built-ins, a note that it's a built-in function. It won't show you code body. That's not a bug; there is no Python body to show.
Getting the source when it's actually Python
Before assuming you're dealing with C code, verify. inspect.getsource() is the direct test:
If that prints readable Python, you're done—you can read the logic, copy it, or add breakpoints with pdb. The same call raises OSError (in older versions, TypeError) when source isn't available, which is your signal to look further.
Two conditions must hold for getsource to work: the object must have a code attribute (functions defined in Python do; C built-ins don't), and the source file must be findable. If the module was imported from a .pyc with no matching .py, or if the file path points somewhere inaccessible, you'll get an error even for pure-Python code. inspect.getfile(target) shows which path Python is trying—check that first.
For a class or method, inspect.getsource(SomeClass.method) works when the method is defined in Python. For properties and decorated functions, unwrap first: inspect.unwrap(obj) or obj.wrapped may reveal the underlying function.
Tracing a built-in to its C source
When getsource fails on a C function like len, list.append, or math.floor, the route is: identify the module, then read the corresponding file in CPython's source distribution.
Here's a reliable sequence:
1: Print target.module and target.name. For math.floor, that's math and floor. 2: Note that the public module often re-exports from a private C module. math re-exports most of _math; collections uses _collections; socket uses _socket. Check target.module on the underlying object if you have access to it. 3: Open the CPython repository (or a tarball of the matching version) and look under Modules/module.c, e.g. Modules/mathmodule.c, Modules/_collectionsmodule.c. 4: Use the build's argument-parsing code as your map. Modern CPython registers C functions with PyArg_ParseTupleAndKeywords, an argument clinic block, or PyArg_Parse with a format string like "O|OO"—the format codes spell out required and optional arguments, which is often more precise than the docstring.
Argument Clinic is worth calling out because it's where a lot of modern built-ins define their signature. A //[clinic start generated code] block in the .c file contains the canonical signature and parsing code; the human-written declaration above it is the source of truth in the repository. This is why inspect.signature() and the docstring agree closely for recent CPython versions—both are generated from that same declaration.
If you're not sure which CPython version matches your interpreter, python -c "import sys; print(sys.version)" prints it. Read the source for that version, not main, or you may chase functions that have moved or changed signature.
Reading CPython source without building the interpreter
You don't need to compile CPython to read its source. Download the tarball for your exact version from Python's release pages or clone the repository, then search the Modules and Python directories. A quick pattern: len is implemented in Python/bltinmodule.c as builtin_len; sorted and list.sort share logic in Objects/listobject.c; int parsing lives in Objects/longobject.c.
Small functions usually sit near the top of their file as a thin wrapper that calls a helper. builtin_len is a few lines: it checks the type supports length via PyObject_Length and returns the result. That brevity is typical of built-ins—they delegate to generic C helpers rather than doing work inline.
When you want to read a specific function and the codebase is huge, grep is your friend. Two useful searches:
Search for the function's exported name, which usually appears in a method table as a {"name", function, flags} entry. Search for the docstring's distinctive first phrase in quotes; docstrings are duplicated in the C source, and a unique phrase pins the location precisely.
Letting Python tell you where the function lives
Several attributes narrow the search before you open any C file:
| Attribute | What it tells you | Weakness |
|---|---|---|
| module | Public module the object claims | May hide a C re-export |
| name | Exported name | Can differ from C symbol |
| qualname | Nested path for methods | Absent on some built-ins |
| doc | Docstring, often with signature | Can be None |
| inspect.getfile | Source path for Python code | Fails for C built-ins |
| inspect.signature | Parameter list | ValueError on some C functions |
A worked example with list.sort shows the full chain. inspect.signature(list.sort) may fail or return a stub because it's a method descriptor of a C type; list.sort.doc contains a sort(*, key=None, reverse=False) style signature embedded in text. list.sort belongs to the list type in Objects/listobject.c, so you search that file for list_sort and find the stable-sort implementation with its merge logic and temporary array allocation. That's the actual code, and reading it tells you things the docstring won't—like the memory behavior under large inputs.
For something like itertools.chain, inspect.getsource fails, module reports itertools, and the code lives in Modules/itertoolsmodule.c as chain_new and chain_next. You learn from the C source that chain is a lazy iterator that holds a single link to the current input, not a list of inputs—useful if you're checking memory behavior.
Practical checks, edge cases, and limits
A few situations trip people up.
Built-in types vs. instances. list.append is a method descriptor on the type; [].append is a bound method. inspect.getsource on the former usually fails, while the latter has no independent source either—both point to C. Introspect the type, not a throwaway instance, when hunting signatures.
Decorated or wrapped functions. A function built with functools.wraps reports the wrapper's metadata but runs wrapper code. inspect.unwrap() peels the wrappers; check .wrapped chain if getsource returns suspiciously short code.
Frozen or zipped installs. If Python was shipped as a zip or frozen executable, inspect.getsource may fail even for library code because files aren't on disk where expected. sys.path inspection and checking for .py versus .pyc presence usually explains it.
Cython and C extensions from third parties. A function from a compiled third-party package may have doc but no accessible C source. Don't confuse "can't read it" with "it's built-in." Check type() and module; a package like numpy supplies its source separately, and rebuilding is the only way to step through it.
A simple success test for the whole workflow: after running inspect.signature, help, or a successful getsource, you should see either a parameter list (with or without names), a docstring, or readable code. If you see an AttributeError on code and getsource raised an error, the function is C-implemented, and the next step is locating its module file. If you see a signature but getsource fails, you're dealing with a C function that exposes metadata through Argument Clinic—still fine, just go to the source tree.
When nothing resolves—no signature, no docstring, no module, no source—and you need the behavior for a real program, that's the point to stop reverse-engineering and check the documentation or the library's own repository. For security-sensitive built-ins like open, don't infer behavior from a partial reading; the docs state the file mode and buffering rules authoritatively, and the C source changes across versions.
Is this worth doing at all?
Yes, for a specific set of questions. Reading the C source settles matters the docs gloss over: implementation complexity of sorted (TimSort, O(n log n) worst case), whether str.find uses a fast substring search, and how int handles huge-digit arithmetic. It also clarifies error behavior—when ValueError versus TypeError gets raised—because the argument parser and type checks are visible in the source.
It's the wrong tool when you only need the API contract. For call-sequence questions, inspect.signature and help are faster and version-accurate; the C source may reflect optimization tricks you can't rely on. And remember that the C source is not a stable API. A function's internal structure can change between minor CPython releases even when the public signature stays fixed. If your goal is portability, code against documented behavior, not against what the C happens to do in 3.13.
The practical habit: reach for inspect first to confirm what an object is, then read Python source if it exists, and only descend into CPython's C files when the behavior genuinely depends on implementation details you need to understand or reproduce.