处理文档字符串中重复内容的好方法是什么?我有许多函数采用'标准'参数,必须在docstring中解释,但是只编写一次docstring的相关部分会很好,因为这将更容易维护和更新.我天真地尝试了以下内容:
arg_a = "a: a very common argument"
def test(a):
'''
Arguments:
%s
''' % arg_a
pass
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为当我这样help(test)做时,我看不到文档字符串.有没有办法做到这一点?
我想显示我的函数的文档字符串,但如果我使用这样的
@cost_time
def func():
"define ...."
blabla
print func.__doc__
Run Code Online (Sandbox Code Playgroud)
它不会显示docstring,只是因为我使用一些元编程技巧,如何解决这个问题?
我正在研究一个具有许多小函数的模块,但其文档字符串往往很长.文档字符串使得模块工作变得烦人,因为我必须不断滚动一个长文档字符串来查找一些实际代码.
有没有办法让文档字符串与它们记录的函数分开?我真的希望能够在远离代码的文件末尾指定文档字符串,或者甚至更好地在单独的文件中指定.
我想以设置func_doc(作为表达)内 def.
def f():
'''My function help''' #Set the docstring
def g():
"My function " + "help" # An expression, so not read as a docstring
# can I put something here to set the docstring as an expression?
g.func_doc # is None
g.func_doc = "My function " + "help" # This works
Run Code Online (Sandbox Code Playgroud)
这可能吗?
(我可以考虑这样做的两个原因:从模块导入函数(并且您也想导入文档字符串)和使用词法分析器.)
我有一个即将发布的python软件包,所以现在我开始使用vim将文档字符串缓慢添加到文件中。所以对于像
def foo(x, y, z=None, **kwargs):
Run Code Online (Sandbox Code Playgroud)
我必须一整天重复手动输入以下内容
""" foo does this stuff.
Parameters
----------
x:
y:
z: optional
kwargs:
Returns
-------
"""
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以通过定义一些vim宏来动态生成此模板文档字符串(我对此一无所知,因此有关如何启动的建议也很好)。谢谢。
我有一个描述符,可以将方法转换为类级别的属性:
class classproperty(object):
def __init__(self, getter):
self.getter = getter
self.__doc__ = getter.__doc__
def __get__(self, instance, owner):
return self.getter(owner)
Run Code Online (Sandbox Code Playgroud)
像这样使用:
class A(object):
@classproperty
def test(cls):
"docstring"
return "Test"
Run Code Online (Sandbox Code Playgroud)
不过,我现在不能访问的__doc__属性(这是合乎逻辑的,因为访问A.test.__doc__将会获取__doc__的str,因为A.test已经返回"Test".
我的最终目标是我的docstring将出现在sphinx中,因此以任何其他方式检索docstring是不可行的,而不是通过访问attributes __doc__属性.我发现自己想知道这是否有可能以任何方式实现.
我知道property通过在没有实例的情况下调用返回类来解决此问题.但是,显然这与我的目标相冲突.
我开始担心这在Python中是不可能的.
注意:classproperty只要它是稳定的(即不设置__doc__返回的值),我愿意拉出任何特技表演.但是,对用户施加任何负担是不可行的classproperty(即他们应该只使用装饰器并完成它).
我有一些Python的经验,但最近才发现它的广泛使用docstrings.我正在浏览金融市场模拟器(FMS)源代码,当我在PyCharm中打开它时,我看到以下语法突出显示(FMS 中其中一个模块的代码片段的屏幕截图):
为什么">>>"之后的语句突出显示为可执行文件?从我所读到的docstrings,在官方文档和SO上(例如在这里)我认为这些语句不应该执行,但语法突出显示令我感到困惑,让我认为">>>"是一个docstring要执行的代码内的代码标记.或者这只是一个PyCharm'bug'?没有任何文件提及与此相关的任何内容,我担心如果我错过了什么.
PS:对于记录,查看SublimeText中的代码不会重现相同的行为.
我在教程中遇到了以下函数.当我调用该函数时,"This prints a passed string into this function"不打印.为什么函数在调用时不打印这段文本?
def printme(str):
"This prints a passed string into this function"
print str
return
# Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")
Run Code Online (Sandbox Code Playgroud) 我正在使用Sphinx记录Python项目。
该.. csv-table::指令似乎有点不一致。
主要问题是单元格中的新行。还有我可疑的心理健康。
如下代码:
.. csv-table::
:header: Header1, Header2, Header3
A, B, "These lines appear as one line,
even though they are written in two lines."
C, D, "| These lines appear as two lines,
| but they are indented, and my OCD will simply not allow it."
E, F, "| If I continue this line in another line,
it will appear in a new line."
G, H, "If there is a blank line between the two …Run Code Online (Sandbox Code Playgroud) 目标:在Python的vscode中自动生成文档字符串,并根据我的喜好格式化生成的文档字符串。
解决方案:我安装了autoDocstring扩展名。
问题:我不知道如何将生成的文档字符串格式化为所需的格式。在“扩展设置”标题下的描述中,似乎建议您可以使用“ autoDocstring.docstringFormat”设置更改默认格式。我的问题是,如何配置该设置?我环顾四周,找不到解决方案。