Reputation: 132
class Array:
def __init__(self):
self.list = []
def add(self, num):
self.list.append(num)
a = Array()
a.add(1).add(2)
I would like to add number 1, 2 to self.list like this. How can I implement?
Upvotes: 1
Views: 1646
Reputation: 2529
As an alternative approach, why not just let your add
method take a list of values as input? Seems like it would be easier to use like that
def add(self, vals):
self.list += vals
So now you can
a.add([1,2])
Instead of
a.add(1).add(2)
Upvotes: 0
Reputation: 4302
After your insertion returns the instance itself for second operation, then you will have instance itself so you can perform add operation:
def add(self, num):
self.list.append(num)
return self
Upvotes: 1
Reputation: 59274
Return the object itself
def add(self, num):
self.list.append(num)
return self
Upvotes: 1