my question is about class design and my current approach is inspired by this question/answer. Starting from there, I create multiple instances of the class Foobar
which are collected and can be accessed via the class Foobar_Collection
. Additionaly, the class Foo
should have some method which is occasionally called for all instances. Now, conditional on the outcome of the method, is there a way to instantly delete an instance?
class Foobar_Collection(dict):
def __init__(self, *arg, **kw):
super(Foobar_Collection, self).__init__(*arg,**kw)
def foobar(self,*arg,**kw):
foo = Foo(*arg,**kw)
self[foo.name] = foo
return ins
class Foo(object):
def __init__(self,name,something):
self.name = name
self.something = something
# just some toy example method
def myMethod(self,x):
if x < 0:
#delete self
else:
print(x)
EDIT
Here is what I have in mind so far, but this is not "instantly" and it's "outside".
fc_collection = Foobar_Collection()
fc_collection.foobar( 'first', 42 )
fc_collection.foobar( 'second', 77 )
for name in fc_collection:
# x will actually be a class attribute, for simplicity:
if x<0:
del fc_collection[name]
else
print(x)