0

在记录实例变量时,我可以

class Foo:
    def __init__(self):
        self.spam = 4
        """Docstring for instance attribute spam."""

这不适用于并行分配

class Foo:
    def __init__(self):
        self.spam, self.bar, self.moo = 4, 5, 6
        """Docstring for instance attribute spam."""

如何在并行赋值中记录变量?

4

2 回答 2

0

我发现我可以在__init__文档字符串块中使用 Sephix 标记,并使其看起来与使用自动属性几乎相同。

class Foo:
    def __init__(self):
        """
        Something something

        .. py:attribute:: spam

           Something about spam

        .. py:attribute:: bar

           Something about br

        .. py:attribute:: moo

           Something about moo
        """
        self.spam, self.bar, self.moo = 4, 5, 6

这看起来像 http://xrxr.github.io/RapidPygame/levelmgr.html (解释和 draw_list 是使用这种技术完成的。)如果autoclass_content设置为'both' (如果你注意,它们不在正确的字母位置)

这行得通,但我仍然想知道是否有更好的方法。

于 2014-06-04T19:35:07.140 回答
0

autoattribute用于单个属性。这是一种方法,与autoclassor一起使用automodule

class Foo:
    """
    :ivar spam: Description of spam
    :ivar bar: Description of bar
    :ivar moo: Description of moo
    """

    def __init__(self):
        self.spam, self.bar, self.moo = 4, 5, 6

与你得到的相比,输出看起来有点不同autoattribute,但我非常喜欢它。

于 2014-06-04T14:42:47.167 回答