Python可以打印函数定义吗?

问题描述:

在JavaScript中,可以打印出函数的定义。有没有办法在Python中实现这一点? (只是在交互模式中玩耍,我想读一个没有打开()的模块,我只是好奇而已)。Python可以打印函数定义吗?

+0

您有该函数的来源。那有什么问题? – 2009-10-13 23:04:09

+0

从交互模式中,您可以使用帮助(功能)来显示功能的文档字符串。 – monkut 2009-10-14 01:36:50

+0

有这个问题的重复:http://*.com/questions/427453/how-can-i-get-the-source-code-of-a-python-function – 2013-05-14 19:36:52

如果要导入的功能,你可以使用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可以找到源代码(所以没有内置对象,C库,pyc文件等)

+0

运行时创建的函数(包括交互式提示)没有文件或数字,这很有道理 – 2009-10-13 20:57:58

+0

这似乎是我正在寻找的东西。谢谢! – 2009-10-14 13:18:08

+6

如何在当前的交互式Python解释器中打印我之前定义的函数定义?这可能吗? – GL2014 2015-01-13 17:25:00

如果您使用iPython,您可以使用function_name?得到帮助,并function_name??将打印出的来源,如果可以。

+3

+1,ipython是真棒!你可以添加一个链接:http://ipython.scipy.org/ – orip 2009-10-13 20:43:46

+0

有时你需要将函数分隔到另一行来调用?例如model.function?不起作用,但f = model.function; F??作品 – thecheech 2017-04-06 10:22:46

可以使用__doc__关键字:

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

这是描述,而不是定义。 – Triptych 2009-10-13 20:43:22

+0

许多内建(通常,在C的模块中定义的功能),它包括函数签名为好,但不一般。 – u0b34a0f6ae 2009-10-13 20:45:28

+1

而在交互shell“帮助(对象)”将在一个更通航的方式进行显示。 – 2009-10-13 20:49:02

虽然我通常会同意inspect是一个很好的答案,但我不同意你无法获得解释器中定义的对象的源代码。如果您使用中的dill.source.getsource,则可以获得函数和lambda表达式的源,即使它们是交互式定义的。 它也可以从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 

>>> 
+0

有时不直接与工作:**的getSource(创建my_function)**,但我能得到它与**的getSource(my_function.func_code)** – Afflatus 2016-09-14 18:41:33

+0

或交替工作:的getSource(创建my_function .__ code__) – Afflatus 2016-09-14 18:53:48

+0

做不工作空闲 – 2017-11-21 12:33:52

可以在函数中使用__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. 
+0

问题是关于函数定义不是函数docstring。 – 2017-11-21 12:35:35

这是我想出如何去做的方式:

import inspect as i 
    import sys 
    sys.stdout.write(inspect.getsource(MyFunction)) 

这将取出新行字符并很好地打印出该功能