Python可以打印函数定义吗?
在JavaScript中,可以打印出函数的定义。有没有办法用Python实现这一点Python可以打印函数定义吗?,python,Python,在JavaScript中,可以打印出函数的定义。有没有办法用Python实现这一点 (只是在交互模式下玩,我想读一个没有open()的模块。我只是好奇) 如果您正在使用,您可以使用函数名?获取帮助,并且函数名???将打印出源代码(如果可以)。如果您正在导入函数,您可以使用: 这将在交互式提示中起作用,但显然仅适用于导入的对象(不是交互式提示中定义的对象)。当然,只有在Python能够找到源代码的情况下(因此不能在内置对象、C libs、.pyc文件等上),它才会起作用。您可以使用uu doc_u
(只是在交互模式下玩,我想读一个没有open()的模块。我只是好奇) 如果您正在使用,您可以使用
函数名?
获取帮助,并且函数名???
将打印出源代码(如果可以)。如果您正在导入函数,您可以使用:
这将在交互式提示中起作用,但显然仅适用于导入的对象(不是交互式提示中定义的对象)。当然,只有在Python能够找到源代码的情况下(因此不能在内置对象、C libs、.pyc文件等上),它才会起作用。您可以使用uu doc_u_u>关键字:
#print the class description
print string.__doc__
#print function description
print open.__doc__
虽然我大体上同意
inspect
是一个很好的答案,但我不同意您不能获得在解释器中定义的对象的源代码。如果使用dill.source.getsource
from,则可以获取函数和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
>>>
您可以在函数中使用
\uuu doc\uuu
,以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.
我就是这样想的:
import inspect as i
import sys
sys.stdout.write(i.getsource(MyFunction))
这将取出新行字符并很好地打印函数使用帮助(函数)
获取函数描述
您可以阅读有关
help()
的更多信息,这是说明,而不是定义。对于许多内置函数(通常是在C模块中定义的函数),它也包括函数签名,但不是一般的。在交互式shell中“help(object)”将以更易于导航的方式显示。@kaizer函数签名也不是定义。\uuuuu doc\uuuu
实际返回的是代码作者放入doc字符串(三重引号字符串)中的内容。没有更多,也没有更少。我认为这里的定义是模棱两可的。对我来说,这可能意味着在运行时创建的stretchFunctions(包括交互式提示符)中的docstring或代码文本或两者,甚至代码对象也没有文件或行号,这很有意义,这似乎就是我想要的。谢谢打印我前面在当前交互式Python解释器中定义的函数定义怎么样?这可能吗?@GL2014:是的,请看我的答案。我可以确认这个答案并检查。getsource()在Python 3.6.9(Ubuntu)中的交互式(ipython3)定义的函数上有效。您有该函数的源代码。这有什么问题?在交互模式下,您可以使用帮助(函数)来显示函数的文档字符串。这个问题有一个重复:有时它不能直接与:getsource(my_函数)一起工作,但我可以让它与getsource(my_函数.func_代码)一起工作有时您需要将函数分成另一行来调用?e、 g.模型.功能??不起作用,但f=model.function;F工作问题是关于函数定义而不是函数docstring。这里的示例不正确,应该是i.getsource(MyFunction)。我没有在帮助中定义源代码。
import inspect as i
import sys
sys.stdout.write(i.getsource(MyFunction))