ObjectA calling ObjectB

Miki Tebeka miki.tebeka at zoran.com
Sun Dec 28 14:21:06 CET 2003


Hello Midas,
> Part 1)  Create 10 new assorted objects
> Part 2)  Link up the objects, somehow, so they can communicate with each other directly, like parts of an electrical circuit.
> Part 3)  Call the first object, which calls another, etc. like an electrical circuit.
> 
> To change the circuit, I would only change part 2 and perhaps part 3.
> 
> For example, ObjectX could have an input, for a string, and two outputs.
> If the input is "TurnOnS", it calls ObjectY.Input3
> If the input is "TurnOnT", it calls ObjectZ.Input7
> 
> ObjectX would call either, ObjectY.Input3 or ObjectZ.Input7, because it was connected that way in part 2.
> 
> I could then later change part 2 so ObjectX called either ObjectE.Input2 or ObjectF.Input9
> In other words, I could "re-wire" the circuit without changing anything in the objects.

>>> class Wired:
	def __init__(self):
		self.sisters = {}
	def register(self, msg, func, *args):
		self.sisters[msg] = (func, args)
	def process(self, msg):
		func, args = self.sisters[msg]
		func(*args)

		
>>> w1 = Wired()
>>> w2 = Wired()
>>> def p(msg):
	print msg
	
>>> w2.woof = lambda m: p("Woof: %s" % m)
>>> 
>>> w2.woof("X")
Woof: X
>>> w1.register("woof", w2.woof, "HELLO")
>>> 
>>> w1.process("woof")
Woof: HELLO
>>> 

HTH.
Miki




More information about the Python-list mailing list