python:继承自内置类型列表,如何访问new class中的元素?

python: inherited from builtin type list, how to access elements within new class?

我想重用内置 list() 类型的一些功能,因此创建了一个继承自它的新 class。现在我想访问列表中的项目。但是怎么办?

我可以创建一个 class,其中包含一个内部列表,然后我可以对其进行处理;但我非常不希望重写所有魔术函数来模仿列表行为。

class NamedList(list):
    def __init__(self, name, *args, **kwargs):
        self.name = name
        super(NamedList, self).__init__(*args, **kwargs)

    def tell_me(self):
        print self.name
        print 'all I contain is' 
        for item in ???: #what belongs here?
            print item

list 的子类和其他内置可迭代类型是可迭代的。

只需使用self:

def tell_me(self):
    print self.name
    print 'all I contain is' 
    for item in self:
        print item