user1050619
user1050619

Reputation: 20856

Overriding list method

I'm trying to override a list method and append 2 elements. How can I do that?

class LI(list):
    def append(self, item):
        self.append(item)


l = LI([100, 200])
l.append(302)
l.append(402)

print l

Final output:

[100,200,302,302,402,402]

Upvotes: 0

Views: 2762

Answers (1)

mgilson
mgilson

Reputation: 309889

class LI(list):
    def append(self, *args):
        self.extend(args)

Now you can use it:

a = LI()
a.append(1,2,3,4)
a.append(5)

Or maybe you meant:

class LI(list):
    def append(self, item):
        list.append(self,item)
        list.append(self,item)

But really, why not just use a regular list and extend and append the way they were meant to be used?

a = list()
a.extend((1,2,3,4))
a.append(5)

or

a = list()
item = 1
a.extend((item,item))

Upvotes: 5

Related Questions