I want to add some attributes to the built-in list
type, so I wrote this:
class MyList(list):
def __new__(cls, *args, **kwargs):
obj = super(MyList, cls).__new__(cls, *args, **kwargs)
obj.append('FirstMen')
return obj
def __init__(self, *args, **kwargs):
self.name = 'Westeros'
def king(self):
print 'IronThrone'
if __name__ == '__main__':
my_list = MyList([1, 2, 3, 4])
print my_list
but my_list
contains only the element 'FirstMen'
. Why my __new__
doesn't work here? And how should I inherit from a built-in type like list
? Is it the same for the immutable types like str
?