使用带有装饰器的docstrings时遇到问题.给出以下示例:
def decorator(f):
def _decorator():
print 'decorator active'
f()
return _decorator
@decorator
def foo():
'''the magic foo function'''
print 'this is function foo'
help(foo)
Run Code Online (Sandbox Code Playgroud)
现在,帮助没有向我显示foo预期的文档字符串,它显示:
Help on function _decorator in module __main__:
_decorator()
Run Code Online (Sandbox Code Playgroud)
没有装饰器,帮助是正确的:
Help on function foo in module __main__:
foo()
the magic foo function
Run Code Online (Sandbox Code Playgroud)
我知道,该函数foo由装饰器包装,因此函数对象不再是该函数foo.但是,按预期获得文档字符串(和帮助)的好方法是什么?
我有一个带有文档字符串的OO层次结构,它需要与代码本身一样多的维护.例如,
class Swallow(object):
def airspeed(self):
"""Returns the airspeed (unladen)"""
raise NotImplementedError
class AfricanSwallow(Swallow):
def airspeed(self):
# whatever
Run Code Online (Sandbox Code Playgroud)
现在,问题是AfricanSwallow.airspeed不继承超类方法的docstring.我知道我可以使用模板方法模式保留文档字符串,即
class Swallow(object):
def airspeed(self):
"""Returns the airspeed (unladen)"""
return self._ask_arthur()
Run Code Online (Sandbox Code Playgroud)
并_ask_arthur在每个子类中实现.但是,我想知道是否还有另一种方法可以继承docstrings,也许还有一些我尚未发现的装饰器?
所以我正在尝试创建一个"动态"文档字符串,如下所示:
ANIMAL_TYPES = ["mammals", "reptiles", "other"]
def func(animalType):
""" This is a sample function.
@param animalType: "It takes one of these animal types %s" % ANIMAL_TYPES
"""
Run Code Online (Sandbox Code Playgroud)
基本上让文档字符串@param animalType显示任何ANIMAL_TYPES有; 这样当更新此变量时,docstring将自动更新.
然而不幸的是,它似乎没有用......有谁知道是否有办法实现这一目标?
我有一个返回函数的包装函数.有没有办法以编程方式设置返回函数的docstring?如果我可以写信,__doc__我会做以下事情:
def wrapper(a):
def add_something(b):
return a + b
add_something.__doc__ = 'Adds ' + str(a) + ' to `b`'
return add_something
Run Code Online (Sandbox Code Playgroud)
然后我就能做到
>>> add_three = wrapper(3)
>>> add_three.__doc__
'Adds 3 to `b`
Run Code Online (Sandbox Code Playgroud)
但是,由于__doc__是只读的,我不能这样做.什么是正确的方法?
编辑:好的,我想保持这个简单,但当然这不是我真正想要做的.虽然一般来说__doc__在我的情况下是可写的但事实并非如此.
我正在尝试unittest自动创建测试用例.我有一个包装函数,它创建一个类对象,它是以下的子类unittest.TestCase:
import unittest
def makeTestCase(filename, my_func):
class ATest(unittest.TestCase):
def testSomething(self):
# Running test in here with data in filename and function my_func
data = loadmat(filename)
result = my_func(data)
self.assertTrue(result > 0)
return ATest
Run Code Online (Sandbox Code Playgroud)
如果我创建这个类并尝试设置docstring testSomething我得到一个错误: …
我无法弄清楚print(__doc__)脚本开头的操作是什么,就像在这个Scikit示例中一样.
我一直在谷歌中寻找Python文档字符串,在__doc__函数中提供一些文档似乎很有用.但我看不到__doc__脚本中间做了什么.
我想在Python文档字符串的其他地方引用先前记录的函数参数.考虑以下(当然是完全人为的)示例:
def foo(bar):
"""Perform foo action
:param bar: The bar parameter
"""
def nested():
"""Some nested function that depends on enclosing scope's bar parameter.
I'd like to reference function foo's bar parameter here
with a link, is that possible?"""
return bar * bar
# ...
return nested()
Run Code Online (Sandbox Code Playgroud)
是否有一种使用Sphinx标记嵌入参数引用的简单方法,还是会自动发生?
(我是一个完整的Sphinx新手.我一直在扫描Sphinx文档,但没有找到这个问题的答案,或者是一个展示正确标记的例子.)
我使用PyCharm IDE来协助制作符合PEP0257标准的文档字符串.它提供了两个属性,我不完全理解它们之间的区别/用法:
:raise Exception: exception explanation here:raises Exception: exception explanation here我什么时候会在我的文档中使用raise反对raises?具体来说,如果一个类需要一个未提供的参数并引发一个TypeError,那应该用来记录它?
我如何记录函数返回的tuple方式,使PyCharm能够将其用于类型提示?
举例:
def fetch_abbrev_customer_info(customer_id):
"""Pulls abbreviated customer data from the database for the Customer
with the specified PK value.
:type customer_id:int The ID of the Customer record to fetch.
:rtype:???
"""
... magic happens here ...
return customer_obj.fullname, customer_obj.status #, etc.
Run Code Online (Sandbox Code Playgroud) 我对python中docstrings和comments之间的区别感到有点困惑.
在我的课堂上,我的老师介绍了一种被称为"设计方法"的东西,这一系列步骤可以帮助我们学生在Python中更好地绘制和组织编码.根据我的理解,下面是我们遵循的步骤示例 - 这就是呼叫设计配方(引文中的内容):
def term_work_mark(a0_mark, a1_mark, a2_mark, ex_mark, midterm_mark):
''' (float, float, float, float, float) -> float
Takes your marks on a0_mark, a1_mark, a2_mark, ex_mark and midterm_mark,
calculates their respective weight contributions and sums these
contributions to deliver your overall term mark out of a maximum of 55 (This
is because the exam mark is not taken account of in this function)
>>>term_work_mark(5, 5, 5, 5, 5)
11.8
>>>term_work_mark(0, 0, 0, 0, 0)
0.0
'''
a0_component = contribution(a0_mark, a0_max_mark, …Run Code Online (Sandbox Code Playgroud) 如何将文档字符串和/或注释作为一个整体添加到Clojure库/命名空间,即不仅仅是命名空间中的特定函数?
我注意到clojure源(comment ...)在某些地方用来做这个(例子),是推荐的吗?