user7742265
user7742265

Reputation:

Alternative for append()

For the below add method:

def __add__(self, other):
    self.str_list.append(other)       
    self.count += 1
    return self.str_list

How may I rewrite this without append?

PS:

1) str_list is an instance variable of type list declared in init

2) No inbuilt functions to be used.

Upvotes: 1

Views: 19975

Answers (1)

EyuelDK
EyuelDK

Reputation: 3189

It's an interesting brain teaser, the challenge being adding to a list without using a built-in function. I can only think of one approach:

self.str_list = [*self.str_list, other]       

Other approaches such as list += [other] are technically using the built-in method __add__ so beware for your purpose, whatever it might be.

Upvotes: 3

Related Questions