Finding the source code for built-in Python functions?

前端 未结 8 1644
一向
一向 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:01

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

    For Python modules, the typical layout is:

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

    For extension-only modules, the typical layout is:

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

    For builtin types, the typical layout is:

    Objects/object.c
    Lib/test/test_.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
    

提交回复
热议问题