如何检查python方法的源代码? [英] How to check source code of a python method?
问题描述
简而言之,假设我有一个字符串'next'
,
To be short, suppose I have a string 'next'
,
next = "123rpq"
,我可以将str
方法.isdigit()
应用于'next'
.所以我的问题是如何检查此方法的实现.因此,是否有类似inspect.getsource(somefunction)
这样的东西来获取方法"的源代码?
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'?
更新:请参见下面有关变量名'next'
的注释.
UPDATE: see comment about variable name 'next'
below.
推荐答案
对于模块,类,函数和其他一些对象,可以使用inspect.getfile
或inspect.getsourcefile
.但是,对于内置对象和方法,这将导致TypeError
.正如C0deH4cker提到的那样,内置对象和方法是用C实现的,因此您必须浏览C源代码. isdigit
是内置字符串对象的方法,该方法在文件 stringobject.c
在Python源代码的Objects
目录中.此isdigits
方法是从此文件的3392行实现的.另请参阅我的答案此处,以回答类似但更笼统的问题.
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.
这篇关于如何检查python方法的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!