Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check source code of a python method?

Tags:

python

methods

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

next = "123rpq"

and I can apply a str method .isdigit() to 'next'. So my question is how to check the implementation of this method. Hence is there a thing like inspect.getsource(somefunction) to get the source code of a 'method'?

UPDATE: see comment about variable name 'next' below.

like image 255
swoopin_swallow Avatar asked Jun 28 '12 22:06

swoopin_swallow


2 Answers

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 unicodeobject.c in the Objects directory of the Python source code. This isdigit method is implemented from line 11,416 of this file. See also my answer here to a similar but more general question.

like image 79
Chris Avatar answered Nov 07 '22 11:11

Chris


The isdigit() method you are talking about is a builtin method of a builtin datatype. Meaning, the source of this method is written in C, not Python. If you really wish to see the source code of it, then I suggest you go to http://python.org and download the source code of Python.

like image 44
C0deH4cker Avatar answered Nov 07 '22 10:11

C0deH4cker