File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed
Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments