How To Call A Different Instance Of The Same Class In Python?
Solution 1:
It is not common for instances of a class to know about other instances of the class.
I would recommend you keep some sort of collection of instances in your class itself, and use the class to look up the instances:
classMachine(object):
lst = []
def__init__(self, name):
self.name = name
self.id = len(Machine.lst)
Machine.lst.append(self)
m0 = Machine("zero")
m1 = Machine("one")
print(Machine.lst[1].name) # prints "one"
Solution 2:
This is a silly example that I cooked up where you put some data into the first machine which then moves it to the first buffer which then moves it to the second machine ...
Each machine just tags the data with it's ID number and passes it along, but you could make the machines do anything. You could even register a function to be called at each machine when it gets data.
classMachine(object):
def__init__(self,number,next=None):
self.number=number
self.register_next(next)
defregister_next(self,next):
self.next=nextdefdo_work(self,data):
#do some work here
newdata='%s %d'%(str(data),self.number)
if(self.nextisnotNone):
self.next.do_work(newdata)
classBuffer(Machine):
def__init__(self,number,next=None):
Machine.__init__(self,number,next=next)
data=Nonedefdo_work(self,data):
if(self.nextisnotNone):
self.next.do_work(data)
else:
self.data=data
#Now, create an assembly line
assembly=[Machine(0)]
for i in xrange(1,20):
machine=not i%2
assembly.append(Machine(i) if machine else Buffer(i))
assembly[-2].register_next(assembly[-1])
assembly[0].do_work('foo')
print (assembly[-1].data)
EDIT
Buffers are now Machines too.
Solution 3:
Now that you added more info about the problem, I'll suggest an alternate solution.
After you have created your machines, you might want to link them together.
classMachine(object):
def__init__(self):
self.handoff = Nonedefinput(self, item):
item = do_something(item) # machine processes item
self.handoff(item) # machine hands off item to next machine
m0 = Machine()
m1 = Machine()
m0.handoff = m1.input
m2 = Machine()
m1.handoff = m2.inputdefoutput(item):
print(item)
m2.handoff = output
Now when you call m0.input(item)
it will do its processing, then hand off the item to m1
, which will do the same and hand off to m2
, which will do its processing and call output()
. This example shows synchronous processing (an item will go all the way through the chain before the function calls return) but you could also have the .input()
method put the item on a queue for processing and then return immediately; in this way, you could make the machines process in parallel.
With this system, the connections between the machines are explicit, and each machine only knows about the one that follows it (the one it needs to know about).
I use the word "threading" to describe the process of linking together objects like this. An item being processed follows the thread from machine to machine before arriving at the output. Its slightly ambiguous because it has nothing to do with threads of execution, so that term isn't perfect.
Post a Comment for "How To Call A Different Instance Of The Same Class In Python?"