This question may sound a little weird, but I have been unable to find the answer so far, and I decided to ask here:
Is there a way to call a method from all the instances of a class without having to loop through each and every single instance manually?
Example:
Normally, I would do something like this:
instList = []
class SomeClass:
def __init__(self,*args,**kwargs):
#init stuff
instList.append(self)
def theThing(self):
#do the thing
def allTheThings(il):
for inst in il:
inst.theThing()
allTheThings(instList)
This works pretty well normally, but it doesn't seem very efficient. Is there some way to streamline this into one or two lines of code?
Something like this:
class SomeClass:
def __init__(self,*args,**kwargs):
#init stuff
def theThing(self):
#do the thing
SomeClass.instances.theThing()
or this:
class SomeClass:
def __init__(self,*args,**kwargs):
#init stuff
def theThing(self):
#do the thing
instancesOf(SomeClass).theThing()
This would be really helpful, as I plan on using this in many different ways: auto-killing everything at the end of a game, detecting various polygon-shaped buttons being clicked, anything that involves calling a single method on a bunch of instances.