Skip to content

Commit 08a7f74

Browse files
committed
raise an exception is the same object is added a second time to a given object list
1 parent 7ba8b7e commit 08a7f74

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

neo/core/objectlist.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ def _handle_append(self, obj):
3232
)
3333
):
3434
raise TypeError(f"Object is a {type(obj)}. It should be one of {self.allowed_contents}.")
35+
36+
if self._contains(obj):
37+
raise ValueError("Cannot add the same object twice")
38+
3539
# set the child-parent relationship
3640
if self.parent:
3741
relationship_name = self.parent.__class__.__name__.lower()
@@ -42,6 +46,13 @@ def _handle_append(self, obj):
4246
# use weakref here? - see https://github.com/NeuralEnsemble/python-neo/issues/684
4347
setattr(obj, relationship_name, self.parent)
4448

49+
def _contains(self, obj):
50+
if self._items is None:
51+
obj_ids = []
52+
else:
53+
obj_ids = [id(item) for item in self._items]
54+
return id(obj) in obj_ids
55+
4556
def __str__(self):
4657
return str(self._items)
4758

0 commit comments

Comments
 (0)