Class 如何将属性的docstring放入cython cdef类?
似乎没有一种简单的方法可以将docstring嵌入到cdef类的属性中。运行常用的object.property时?或ipython中的helpobject.property,它不会显示输入的docstring,而是显示该属性指定给的对象类型的python docstring。这是在.pyx文件顶部的embedsignature=True指令下进行的 我正在编写的代码有许多属性,这些属性都会产生这个问题,因此,这里是到目前为止编写的基本格式:Class 如何将属性的docstring放入cython cdef类?,class,cython,Class,Cython,似乎没有一种简单的方法可以将docstring嵌入到cdef类的属性中。运行常用的object.property时?或ipython中的helpobject.property,它不会显示输入的docstring,而是显示该属性指定给的对象类型的python docstring。这是在.pyx文件顶部的embedsignature=True指令下进行的 我正在编写的代码有许多属性,这些属性都会产生这个问题,因此,这里是到目前为止编写的基本格式: cdef class foo: cdef
cdef class foo:
cdef object _attr
def __init__(self, attr = 0):
self._attr = attr
@property
def attr(self):
"""
The docstring that should be printed.
"""
return self._attr
@attr.setter
def attr(self, value):
self._attr = value
我还尝试了以下不推荐使用的语法,即docstring可以这样编写。尽管不推荐使用,但这仍然表明应该支持cdef类中属性的docstring
cdef class foo:
cdef object _attr
def __init__(self, attr = 0):
self._attr = attr
property attr:
"""
The docstring that should be printed.
"""
def __get__(self):
return self._attr
def __set__(self, value):
self._attr = value
如果我运行bar=foo后跟bar.attr?或ipython中的helpbar.attr,预期的docstring是应打印的docstring。但是,如果可能的话,它会显示Convert a string do a floating point number。这是python提供的浮点类型的docstring
我意识到这个问题的另一种解决方案是简单地使用python类而不是cdef类,但是我正在实现的对象也用C包装了一个typedef struct对象,我已经为它编写了一个扩展的子程序库,也用C编写了一个。这排除了简单的python类包装的可能性
这个问题有简单的解决办法吗?如果不是,我会将其解释为cython实现的问题;用户定义对象的docstring对于python来说相当重要 Cython的工作方式和行为方式与Python完全相同。为了测试,我用普通Python复制了您的示例 如果调用“类”属性的帮助,则可以正确获取文档字符串: helpfoo.attr 应该打印的文档字符串 但是,如果调用实例属性的帮助,则会获得有关int的信息: helpfoo.attr 有关int对象的帮助: 类为对象 这是正确的-foo.attr调用返回整数的属性方法,该整数是传递给帮助的。这个整数无法知道它是从属性返回的,因此它没有意义将docstring与属性关联