如何正确记录 python 枚举元素?
How do I properly document python enum elements?
我知道我可以像添加任何其他 class 一样向枚举类型添加 Python 文档字符串。但是如何向该类型的元素添加文档?
据我所知有三种可能:
class MyEnum(Enum):
"""
This is my enum type.
"""
"""
Variant 1
"""
a = 0,
b = 1, # variant 2
c = 2, """ variant 3 """
但其中 none 确实始终如一地工作。
如果我在任何变体中调用 print(inspect.getdoc(MyEnum.a))
,将返回 MyEnum
类型的文档字符串 ('This is my enum type')。 Pycharm 可以在其快速文档预览中显示变体 3,但包括引号和超出列换行的较长注释将无法正确显示。
是否有关于如何记录 Python 枚举元素的首选方式或约定?
如果值本身不重要,请参阅 How do I put docstrings on Enums?。如果值很重要,您可以自定义该答案或使用 aenum
1 库:
from aenum import Enum
class MyEnum(Enum):
_init_ = 'value __doc__'
a = 0, 'docstring for a'
b = 1, 'another for b'
c = 2, 'and one for c as well'
这导致:
>>> MyEnum.b.value
1
>>> MyEnum.b.__doc__
'another for b'
但是,我不知道哪些 IDE(如果有的话)支持使用 Enum 成员文档字符串。
1 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。
我知道我可以像添加任何其他 class 一样向枚举类型添加 Python 文档字符串。但是如何向该类型的元素添加文档?
据我所知有三种可能:
class MyEnum(Enum):
"""
This is my enum type.
"""
"""
Variant 1
"""
a = 0,
b = 1, # variant 2
c = 2, """ variant 3 """
但其中 none 确实始终如一地工作。
如果我在任何变体中调用 print(inspect.getdoc(MyEnum.a))
,将返回 MyEnum
类型的文档字符串 ('This is my enum type')。 Pycharm 可以在其快速文档预览中显示变体 3,但包括引号和超出列换行的较长注释将无法正确显示。
是否有关于如何记录 Python 枚举元素的首选方式或约定?
如果值本身不重要,请参阅 How do I put docstrings on Enums?。如果值很重要,您可以自定义该答案或使用 aenum
1 库:
from aenum import Enum
class MyEnum(Enum):
_init_ = 'value __doc__'
a = 0, 'docstring for a'
b = 1, 'another for b'
c = 2, 'and one for c as well'
这导致:
>>> MyEnum.b.value
1
>>> MyEnum.b.__doc__
'another for b'
但是,我不知道哪些 IDE(如果有的话)支持使用 Enum 成员文档字符串。
1 披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。