How to Find the source code for built-in Python functions?

Submitted 3 years, 5 months ago
Ticket #295
Views 257
Language/Framework Python
Priority Low
Status Closed

What are the source code for built-in python function like code behind sorted or enumerate etc...?

Submitted on Nov 16, 20
add a comment

1 Answer

Verified

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.

Submitted 3 years, 4 months ago


Latest Blogs