比较以下内容。
示例 1:装饰器之前的文档字符串。
@app.route("/")
"""
summary
"""
def hello() -> str:
return "Hello World"
Run Code Online (Sandbox Code Playgroud)
对比示例 2:装饰器后的文档字符串:
"""
summary
"""
@app.route("/")
def hello() -> str:
return "Hello World"
Run Code Online (Sandbox Code Playgroud) 你怎么能用装饰器添加函数docstring?
def g(func):
someOtherDocString = "That is great"
def wrap(*args, **kwargs):
func(*args, **kwargs)
return wrap
@g
def f():
""" This is awesome """
Run Code Online (Sandbox Code Playgroud)
结果:
>>>help(f)
Help on function f in module __main__:
f()
That is great
That is awesome
Run Code Online (Sandbox Code Playgroud)
所有的帮助将不胜感激.
比方说,我有一个这样的函数:
def myFunc():
# useful function to calculate stuff
Run Code Online (Sandbox Code Playgroud)
这将产生缩进错误,除非我添加pass:
def myFunc():
# useful function to calculate stuff
pass
Run Code Online (Sandbox Code Playgroud)
但是,如果我用docstring替换注释,pass则不需要:
def myFunc():
"""useful function to calculate stuff"""
Run Code Online (Sandbox Code Playgroud)
这看起来像一个奇怪的特征,因为据我所知,这些都没有在程序中使用.那么,为什么它会像这样呢?
我们从Sphinx切换为样式NumPy文档字符串,并且.. sectionauthor::似乎无法正确呈现。但是,我们需要能够为一个类的每个方法分配一个作者。
有没有一种方法可以适当地向NumPy文档字符串添加作者身份?
一个简单的例子:
class A:
def f(self):
""" My function
It does this and that.
Returns
-------
object
.. sectionauthor:: name of the author
Examples
--------
>>> A().f()
"""
pass
Run Code Online (Sandbox Code Playgroud)
编译到此帮助文档中(注意问题返回:部分):
PS .. sectionauthor::在NumPy docstring中建议在其他地方使用(无法回忆起源代码),因此也许根本就没有正确使用/放置它。
我觉得这是一个愚蠢的问题,但当我在一个IPython笔记本中,我确实帮助了一些numpy/scipy函数,比如说stat.norm.rvs,它经常说,关于*args和**kargs,"有关更多信息,请参阅实例对象的docstring".如果没有帮助(stat.norm.rvs),我如何看到这个docstring?
我在 python 中有很长的源代码,许多函数都用'''xxx''',如何"""xxx"""快速替换它们?在任何文本编辑器中?pycharm / 崇高
def my_func():
'''
yeah a doc string
'''
pass
Run Code Online (Sandbox Code Playgroud)
想要的结果:
def my_func():
"""
yeah a doc string
"""
pass
Run Code Online (Sandbox Code Playgroud)
编辑:在 pycharm(或其他所有文本编辑器)中找到解决方案搜索
'''(\n.*\n\s*)'''\n
并替换为
"""$1"""\n
ps:我不能简单地搜索''',然后用 替换它""",因为在代码中多行字符串无处不在,而不仅仅是在文档字符串中。
这些命令都不会检索函数的文档字符串并将其分配给变量.如何实现?
我尝试过各种各样的事 其中一个是help函数,但它似乎激活整个程序而不是返回一个字符串.我尝试了各种命令,但没有一个能够检索文档字符串.
import PIL
PILCommands=dir('PIL')
ListA=[]
ListB=[]
ListC=[]
ListD=[]
ListE=[]
LisfF=[]
ListG=[]
ListH=[]
for x in PILCommands:
print(x)
try:
ListA.append(x.__doc__)
except:
pass
try:
ListB.append(x.__doc__())
except:
pass
try:
ListC.append(str(x))
except:
pass
try:
ListD.append(help(x))
except:
pass
try:
ListE.append(eval("x.__doc__"))
except:
pass
try:
ListF.append(eval("inspect.getdoc(x)"))
except:
pass
try:
ListG.append(eval("dir(x)"))
except:
pass
try:
ListH.append(eval("PIL.x.__doc__"))
except:
pass
print
print("Command1: x.__doc__")
print(ListA)
print
print("Command1: x.__doc__()")
print(ListB)
print
print("Command1: str(x)")
print(ListC)
print
print("help(x)")
print(ListD)
print
print('Command1: eval("eval("x.__doc__")')
print(ListE)
print
print('Command1: eval("inspect.getdoc(x)")')
print(ListE)
print
print('Command1: eval("dir(x)")')
print(ListG) …Run Code Online (Sandbox Code Playgroud) 是否有可能将图片嵌入到Python的docstring中?
我在docpicture某处看到了关键字,但找不到文档.它是否是坚定的?
图片应该放在哪里?
UPDATE
我试过Sphinx符号,但我的PyCharm显示
即它看到它应该是一个图像,但没有看到图像文件.
如果使用Sphinx我会放在哪里?
假设我有一个返回多个值的函数
import numpy as np
def add_and_raisepower(x, y):
"""
1) Add inputs **x** and **y** and return the result.
2) Raise **x** to the power of **y**, multiply this
result by an array of one's and return the result.
:type x: float
:param x: The first input number
:type y: float
:param y: The second input number
:rtype val1: float
:rtype val2: numpy.array(float)
"""
val1 = x + y
val2 = np.ones(100)*(x**y)
return val1, val2
Run Code Online (Sandbox Code Playgroud)
我关心的是:rtype:文档字符串中的评论;如果函数返回多个值(如本示例所示),则应如何:rtype:在文档字符串中编写(根据 …
我在 python 3.x 中有一个函数
def foo():
"""Lorem ipsum for more info, see here""
Run Code Online (Sandbox Code Playgroud)
我想添加一个指向“此处”的超链接以指向网站。在不安装外部插件的情况下如何做到这一点?