Python能打印一个函数定义吗?

在JavaScript中,可以打印出一个函数的定义。 有没有办法在Python中完成这个?

(只是在交互模式下玩耍,我想读一个没有打开()的模块,我只是好奇)。

如果您正在导入函数,则可以使用inspect.getsource

 >>> import re >>> import inspect >>> print inspect.getsource(re.compile) def compile(pattern, flags=0): "Compile a regular expression pattern, returning a pattern object." return _compile(pattern, flags) 

在交互式提示符下工作,但显然只在导入的对象上(而不是在交互式提示符中定义的对象)工作。 当然,只有Python可以find源代码(所以不在内置对象,C库,.pyc文件等)

如果你使用iPython ,你可以使用function_name? 获得帮助,和function_name?? 将打印出来,如果可以的话。

虽然我通常会同意inspect是一个很好的答案,但我不同意你不能得到解释器中定义的对象的源代码。 如果您使用dill.source.getsource ,即使它们是交互式定义的,您也可以获得函数和lambdaexpression式的来源。 它也可以从curries中定义的绑定或非绑定类方法和函数获取代码…但是,如果没有包含对象的代码,您可能无法编译该代码。

 >>> from dill.source import getsource >>> >>> def add(x,y): ... return x+y ... >>> squared = lambda x:x**2 >>> >>> print getsource(add) def add(x,y): return x+y >>> print getsource(squared) squared = lambda x:x**2 >>> >>> class Foo(object): ... def bar(self, x): ... return x*x+x ... >>> f = Foo() >>> >>> print getsource(f.bar) def bar(self, x): return x*x+x >>> 

看看来自pydoc模块的help()函数。 在交互模式下,它应该已经为你导入,所以只需键入help(funtion_to_describe) 。 有关更多function,请使用IPython 。

你可以在函数中使用__doc__ ,以hog()函数为例:你可以这样看到hog()的用法:

 from skimage.feature import hog print hog.__doc__ 

输出将是:

 Extract Histogram of Oriented Gradients (HOG) for a given image. Compute a Histogram of Oriented Gradients (HOG) by 1. (optional) global image normalisation 2. computing the gradient image in x and y 3. computing gradient histograms 4. normalising across blocks 5. flattening into a feature vector Parameters ---------- image : (M, N) ndarray Input image (greyscale). orientations : int Number of orientation bins. pixels_per_cell : 2 tuple (int, int) Size (in pixels) of a cell. cells_per_block : 2 tuple (int,int) Number of cells in each block. visualise : bool, optional Also return an image of the HOG. transform_sqrt : bool, optional Apply power law compression to normalise the image before processing. DO NOT use this if the image contains negative values. Also see `notes` section below. feature_vector : bool, optional Return the data as a feature vector by calling .ravel() on the result just before returning. normalise : bool, deprecated The parameter is deprecated. Use `transform_sqrt` for power law compression. `normalise` has been deprecated. Returns ------- newarr : ndarray HOG for the image as a 1D (flattened) array. hog_image : ndarray (if visualise=True) A visualisation of the HOG image. References ---------- * http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients * Dalal, N and Triggs, B, Histograms of Oriented Gradients for Human Detection, IEEE Computer Society Conference on Computer Vision and Pattern Recognition 2005 San Diego, CA, USA Notes ----- Power law compression, also known as Gamma correction, is used to reduce the effects of shadowing and illumination variations. The compression makes the dark regions lighter. When the kwarg `transform_sqrt` is set to ``True``, the function computes the square root of each color channel and then applies the hog algorithm to the image. 

您可以使用__doc__关键字:

 #print the class description print string.__doc__ #print function description print open.__doc__