mercredi 18 septembre 2019

Keep track of class instances pointing to updated list

I am new to stackoverflow and to OOP as well,

The problem: I have a class TrackList which is supposed to keep track of the instances of Element. Element is a class which uses webdriver to find an item of a list based on the index. The list has one item list[0].

class TrackList():
  def __init__(self):
    self.instance_list = {}

  def element_instance(self, index):
    if index not in self.instance_list:
      self.instance_list[index] = Element(index)
    return self.instance_list[index]

class Element():
  def __init__(self, index):
    self.index = index

  def _web_driver_search(self):
    #find a node based on the index provided i.e.
    driver.find_element(By.XPATH, f'//li[text()="{self.index}"]')
    print("new element found")

  ...


Now let's assume that the list gets updated, so the object which were in list[0] = list[1] and a new object becomes list[0]. Unfortunately the current implementation will still return me the old object since instance_list[0] exists. How can I achieve that instance_list[0] points to the new object and instance_list[1] to the old one? Am I missing something or the design pattern is wrong?

Aucun commentaire:

Enregistrer un commentaire