people. I’m python newbie. I have two def functions as below under a class.
def add_item(self, itemID, itemlist):
lines = []
self.itemID = itemID
self.itemlist = itemlist
for line in self.itemID, itemlist:
lines.append(line)
and
def get_keys(self):
i = []
i.append(self.itemID)
return i
If I do
example.add_item('abc', item list)
example.add_item('abcd', item list)
example.add_item('abce', item list)
then when I do
example.get_keys()
It should give:
['abc', 'abcd', 'abce']
but mine only gives the latest one that is ['abce'].
Can anyone please let me know how to fix?
If I understand correctly, you want to add several couple of
keyanditem_listto your example, and be able to retrieve the keys you added so far ? The easiest is to store thekeysand theitemlistin two listsAssuming that you initialize your object as such
Now, your
add_itemcan simplify inand your
get_keyis only:Note that the
get_keyis exactly the one you have suggested, just simpler (no need to create a temporary list).When you do
linefirst takes the valueself.itemID, thenitemlist. Eventually, yourlinesis just[self.itemID, itemlist]. Probably not what you had in mind.