How to check source code of a python method?

前端 未结 5 1660
遥遥无期
遥遥无期 2021-01-12 09:14

To be short, suppose I have a string \'next\',

next = \"123rpq\"

and I can apply a str method .isdigit()

5条回答
  •  青春惊慌失措
    2021-01-12 09:58

    For modules, classes, functions and a few other objects you can use inspect.getfile or inspect.getsourcefile. However, for builtin objects and methods this will result in a TypeError. As metioned by C0deH4cker, builtin objects and methods are implemented in C, so you will have to browse the C source code. isdigit is a method of the builtin string object, which is implemented in the file stringobject.c in the Objects directory of the Python source code. This isdigits method is implemented from line 3392 of this file. See also my answer here to a similar but more general question.

提交回复
热议问题