2009-08-28 66 views
5

下面的示例取自“潜入python”一书。记录类属性

class MP3FileInfo(FileInfo): 
    "store ID3v1.0 MP3 tags" 
    tagDataMap = ... 

此示例显示记录MP3FileInfo,但是如何向MP3FileInfo添加帮助。 tagDataMap地图

回答

1

将其更改为属性方法。

+1

我想知道这一点,因为那时你必须创建明确的getter,setter和deleter(我希望所有三个),只有让它行为正常。有没有一种方法来设置这些只包装一个类属性的默认方法? – u0b34a0f6ae

+0

只需在您的类文档字符串中提及tagDataMap。 – aehlke

+0

...通过使用多行文档字符串:'''富酒吧''' – aehlke

4

关于属性docstrings的PEP 224被拒绝(很久以前),所以这对我来说也是一个问题,有时我不知道要选择类属性还是实例属性 - 第二个可以有文档字符串。

0

做这样的:

class MP3FileInfo(FileInfo): 
    """Store ID3v1.0 MP3 tags.""" 

    @property 
    def tagDataMap(self): 
     """This function computes map of tags. 

     The amount of work necessary to compute is quite large, therefore 
     we memoize the result. 

     """ 
     ... 

不过要注意你真的不应该让一个单独的文档字符串如果属性只有一个行描述。相反,使用

class MP3FileInfo(FileInfo): 
    """Store ID3v1.0 MP3 tags. 

    Here are the attributes: 
     tagDataMap -- contains a map of tags 

    """ 

    tagDataMap = ... 
+0

在我的情况属性具有公共可见性 - 这就是为什么帮助(tagDataMap)是我想要的 – Dewfy