Finding the source code for built-in Python functions?

how to see the code of a function in python
python source code
python source code c
python built-in functions
python builtin functions source code
how to read source code in python
python function code
python code object

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...?

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.

How to retrieve source code of Python functions, I am new to Python. I just want to know how to get the source code of built-in these functions are written. Any help would be appreciated. dill extends Python's pickle module for serializing and deserializing Python objects to the majority of the built-in Python types. At the same time, it can also retrieve the source code of your Python objects. Please note dill is not a standard library, so you must install it separately. Its API is quite similar to inspect 's.

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.

Is it possible to find the source code of built-in functions of Python , Most of the standard library and user code is implemented in pure Python. Source Excellent answer at Finding the source code for built-in Python functions? Since Python is open source you can read the source code from the official python website 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. answered Jul 17, 2019 by Neel

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.

How to find the source code of a function in Python, The inspect module provides several useful functions to help get information about this module: type checking, getting source code, inspecting classes and functions, They also help you determine when you can expect to find the following Return true if the object is a class, whether built-in or created in Python code. 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.

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

28.13. inspect — Inspect live objects, The Python interpreter has a number of functions and types built into it that are always Code objects can be executed by exec() or eval() . source can either be a the instance dictionary when searching for the value's __format__() method. Python's built-in functions (for cpython) are written in C, so vscode presents you dummy methods that show only the function signatures. If you'd like to view the source code for some built-in methods, you'll have to proceed to the GitHub page with the source code:

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  --

Built-in Functions, to getmembers() . They also help you determine when you can expect to find the following special attributes: code object containing compiled function bytecode. __defaults__ current line number in Python source code. tb_next Return True if the object is a class, whether built-in or created in Python code. inspect. Unfortunately, Python doesn't provide a record of source files for built-in functions. You can find out which module provides the function: >>> import numpy as np >>> np.where <built-in function where> >>> np.where.__module__ 'numpy.core.multiarray' Python won't help you find the native (C) source code for that module, but in this case it's

inspect — Inspect live objects, We use the getsource() method of inspect module to get the source code of the function.inspect.getsource(object)Returns the text of the source  set is a built-in class. See set and Set Types — set, frozenset for documentation about this class. For other containers see the built-in frozenset, list, tuple, and dict classes, as well as the collections module. setattr (object, name, value) ¶ This is the counterpart of getattr(). The arguments are an object, a string and an arbitrary value.

How to retrieve source code from Python objects?, This tutorial will walk you through the CPython source code. You can import built-in modules from the standard library like json . TYPE() is one of the core functions used by the AST to determine what type a node in the  For example, if you type MyLibrary.SayHello.__sizeof__ () and press Enter, you see the size of the SayHello () function in bytes. Unlike many other programming languages, Python also makes the source code for its native language libraries available.

Your Guide to the CPython Source Code – Real Python, Built-in objects#. The source code for Python's built-in functions is written in c and can only be accessed by looking at the Python's source code (hosted  The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.

Comments
  • Can you give an example with enumerate?
  • following the OP, how about the source code for "sorted"? of course, inspect.getsourcefile(sorted) doesn't work.
  • @Quetzalcoatl the source code for sorted() is in /Python/bltinmodule.c although it just calls list.sort() so the real source is in /Objects/listobject.c
  • 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.
  • See an early project for viewing source code of a builtin function: github.com/punchagan/cinspect
  • A list is an object/type, not a builtin function. You can find the implementation details for that in listobject.c github.com/python/cpython/tree/master/Objects
  • The OP specifically wants to look at the code, help gives only documentation.
  • And inspect does not work with builtin functions.