这与How to document a module constant in Python?有关,但不相同。

我在模块中有一个常量(它是字典):

possiblestringencodings = dict(
    StringsAsBytes=1,
    ascii=1,
    utf8=1, utf_8=1, U8=1,
    utf16=2, utf_16=2, U16=2, utf_16_be=2, utf_16_le=2,
    utf32=4, utf_32=4, U32=4, utf_32_be=4, utf_32_le=4,
)


readthedocs页面具有(see autodata docs):

.. autodata:: construct.possiblestringencodings


但是,这会从dict docstring(其ctor)生成docstring。如何仅使用Sphinx记录该词典的内容?

python - Sphinx文档字典内容(模块常量)-LMLPHP

如果有人想测试修补程序,只需派生仓库并在docs /文件夹中运行“ make html”即可。
https://github.com/construct/construct/blob/1b53d9122a2c652db64c6558d101caee5bbbab3a/construct/core.py#L1280
https://github.com/construct/construct/blob/1b53d9122a2c652db64c6558d101caee5bbbab3a/docs/api/strings.rst

最佳答案

字典数据成员没有文档字符串,因此您可以从dict类获得该文档字符串。

在定义之前(或紧随其后的文档字符串)添加一个空的“ documentation comment”,您将仅在输出中获得字典项。

#:
possiblestringencodings = dict(
    StringsAsBytes=1,
    ascii=1,
    utf8=1, utf_8=1, U8=1,
    utf16=2, utf_16=2, U16=2, utf_16_be=2, utf_16_le=2,
    utf32=4, utf_32=4, U32=4, utf_32_be=4, utf_32_le=4,
)


您还需要完全限定“核心”模块:

.. autodata:: construct.core.possiblestringencodings

关于python - Sphinx文档字典内容(模块常量),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48565815/

10-10 17:05