Python Forum
Problem in list manipulation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in list manipulation
#1
Error in list processing??

Class Diagram:

Bindable (my class)         list (built in)
        \                                  /
         +------------+----------+
                           |
                           V
                    BindableList

class Bindable:
    def __init__(self):
        super().__init__()
        self._binds = {}

    def bind(self, event, callback):
        if event not in self._binds.keys:
            self._binds[event] = []
        self._binds[event].append(callback)

    def event_generate(self, event_id, *params):
        for callback in self._binds[event_id]:
            callback(event_id, self, *params)

class BindableList(Bindable, list):
    def __init__(self):
        super().__init__()

    def append(self, __object) -> None:
        super().append(__object)
        self.event_generate(BindableList.ITEM_APPENDED, __object)

    def insert(self, index, __object) -> None:
        super().insert(index, __object)
       self.event_generate(BindableList.ITEM_INSERTED, index, __object)

    def remove(self, __object) -> None:
        super().remove(__object)
        self.event_generate(BindableList.ITEM_REMOVED, __object)
The purpose of BindableList is to keep a tkinter.ttk.Treeview synchronized with the various lists, even if the list is modified directly by someone else (i.e. a variation on the observer design pattern).

Here's the problem:

if I have a straight list:

my_list = list()
my_list.append(object1)
my_list.append(object2)
my_list.append(object3)

my_list.remove(object2)
all works fine. The resulting list only has object1 and object3 in it. HOWEVER, if I do:

my_list = BindableList()
my_list.append(object1)
my_list.append(object2)
my_list.append(object3)

my_list.remove(object2)
then my_list always removed the first item in the list - i.e. the result of the above code is that my_list contains object2 and object3

Version of python is 3.9.5 running on MS-Windows (the PyCharm IDE, if that matters)

Anyone see anything obvious that I'm messing up?
Reply


Messages In This Thread
Problem in list manipulation - by CyKlop - Oct-17-2021, 04:41 PM
RE: Problem in list manipulation - by bowlofred - Oct-17-2021, 09:00 PM
RE: Problem in list manipulation - by CyKlop - Oct-17-2021, 09:54 PM
RE: Problem in list manipulation - by deanhystad - Oct-18-2021, 03:42 AM
RE: Problem in list manipulation - by CyKlop - Oct-18-2021, 04:27 AM
RE: Problem in list manipulation - by deanhystad - Oct-18-2021, 08:44 AM
RE: Problem in list manipulation - by DeaD_EyE - Oct-18-2021, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,165 Apr-12-2022, 12:25 AM
Last Post: Pnerd
  optimization problem for dataframe manipulation fimmu 0 1,493 Aug-31-2020, 06:02 PM
Last Post: fimmu
  How to pass a list by value for manipulation within a process? bRitch022 4 2,769 Jul-09-2020, 07:13 PM
Last Post: bRitch022
  IP string manipulation problem TheRealNoob 12 7,419 Feb-04-2019, 09:29 AM
Last Post: perfringo
  list manipulation cameronwood611 3 3,615 Oct-03-2017, 02:58 PM
Last Post: ichabod801
  list or dictionary manipulation dtigue 5 109,418 Jul-21-2017, 03:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020