Python inheritance from lists

This question already has an answer here:

  • Understanding Python super() with __init__() methods [duplicate] 7 answers

  • You need to make a super call to instantiate the object using the parents __init__ method first:

    class Person(list):
        def __init__(self, a_name, a_dob=None, a_books=[]):
            #person initialization code
            super(Person, self).__init__(a_books)
            self.name = a_name
            self.dob = a_dob
    
    print Person('bob',a_books=['how to bob'])
    

    Gives the output:

    ['how to bob']
    

    Because list has a __str__ method.

    链接地址: http://www.djcxy.com/p/30046.html

    上一篇: 如何在python中引用父方法?

    下一篇: Python从列表继承