我有一个像这样开始的函数:
def apply_weighting(self, weighting):
"""
Available functions: {}
""".format(weightings)我想要的是docstring打印可用加权函数的字典。但是,在检查函数时,它声明没有可用的docstring:
In [69]: d.apply_weighting?
Type: instancemethod
String Form:<bound method DissectSpace.apply_weighting of <dissect.DissectSpace instance at 0x106b74dd0>>
File: [...]/dissect.py
Definition: d.apply_weighting(self, weighting)
Docstring: <no docstring>怎么会这样?不可能格式化一个docstring吗?
发布于 2014-02-17 20:26:31
Python解释器只查找字符串文本。不支持添加.format()方法调用,而不支持函数定义语法。解析docstring的是编译器,而不是解释器,任何像weightings这样的变量当时都不可用;此时不执行任何代码。
您始终可以在以下事实之后更新docstring:
def apply_weighting(self, weighting):
"""
Available functions: {}
"""
apply_weighting.__doc__ = apply_weighting.__doc__.format(weightings)https://stackoverflow.com/questions/21838624
复制相似问题