我想以编程方式获取给定类属性(例如pandas.DataFrame.iloc
)的源代码。我尝试使用inspect.findsource()
,它对类和函数都适用。但是,它不适用于属性。
import pandas
import inspect
type(pandas.DataFrame) # type
inspect.findsource(pandas.DataFrame) # works ok
type(pandas.DataFrame.apply) # function
inspect.findsource(pandas.DataFrame.apply) # works ok
type(pandas.DataFrame.iloc) # property
inspect.findsource(pandas.DataFrame.iloc) # throws exception
TypeError: module, class, method, function, traceback, frame, or code object was expected, got property
为什么inspect
找不到财产来源?如何以编程方式获取给定属性的源代码(或源文件路径)?