python(ipython)中如何判断一个方法来自哪里

How to determine where a method came from in python (ipython)

我有一个 ipython 笔记本,在 第一个单元格。

import pandas
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 
....

然后,还有call函数call在后面。

###.plot()

如何确定 .plot() 的来源?

使用 ?? 将为您提供有关对象的简短附加信息,例如它们被定义的地方。

>>> plt.plot?
Signature: plt.plot(*args, **kwargs)
Source:
@_autogen_docstring(Axes.plot)
...
File:      /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/pyplot.py
Type:      function

一般来说,您可以使用特殊属性 __package____module__ 来找出来源。

>>> plt.__package__
'matplotlib'
>>> time.__module__
'time'

请注意,如果您不知道自己在处理什么,您可能必须捕获 AttributeError,因为只有模块定义 __package__,而只有函数定义 __module__