Finding the source code for built-in Python functions?

前端 未结 8 1652
一向
一向 2020-11-22 02:40

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

相关标签:
8条回答
  • 2020-11-22 03:10

    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.

    0 讨论(0)
  • 2020-11-22 03:12

    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.

    0 讨论(0)
提交回复
热议问题