Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 跟踪指向更新列表的类实例_Python_Selenium_Oop_Design Patterns - Fatal编程技术网

Python 跟踪指向更新列表的类实例

Python 跟踪指向更新列表的类实例,python,selenium,oop,design-patterns,Python,Selenium,Oop,Design Patterns,我对stackoverflow和OOP都是新手 问题是:我有一个类TrackList,它应该跟踪元素的实例Element是一个使用webdriver根据索引查找列表项的类。该列表有一个项目列表[0] class TrackList(): def __init__(self): self.instance_list = {} def element_instance(self, index): if index not in self.instance_list:

我对stackoverflow和OOP都是新手

问题是:我有一个类
TrackList
,它应该跟踪
元素的实例
Element
是一个使用webdriver根据索引查找列表项的类。该列表有一个项目列表[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")

  ...


现在让我们假设列表已更新,因此列表[0]=列表[1]中的对象和新对象将成为列表[0]。不幸的是,由于实例列表[0]存在,当前实现仍将返回旧对象。如何实现实例列表[0]指向新对象,实例列表[1]指向旧对象?我遗漏了什么或者设计模式有问题吗?

instance\u list={}
创建了一个空的dict(dictionary/map),而不是一个列表,因此如果您想要的话,可以更清楚地称之为
instance\u dict

听起来您想使用
堆栈
,但如果不知道web驱动程序列表是如何工作的,我不能肯定

如果web驱动程序添加另一个元素时索引的含义发生变化,则不存储索引,只需通过索引从
数组中获取元素即可。同样,我取决于web驱动程序做什么,您需要模仿这种行为