[Pythonmac-SIG] self-instantiating instances in pyhton

Rayme Jernigan rayme at pobox.com
Wed Jan 12 18:52:54 CET 2005


Hi,

I'm looking for a language to abuse for a project I'm working on. I 
know a little Python, but I do not know if it can do this: I'd like to 
define some class "N" that instantiates objects that can instantiate 
fresh new objects of that same class... without external controller 
code.

You could use this capability, for example, to generate a red-black 
tree-type data structure that implicitly instantiates a new node for 
each new input d1, d2, d3... so on. The algorithm for each node could 
be:

-- Toggle downstream L/R pointer
-- If no node there, instantiate one and link to it
-- if a node is there, pass the datum to it

The execution sequence as the tree builds itself out might look 
something like this:

1. n0 = N(d0)	# new instance of N
			# (downstream pointer default = "Left")
			# (the first input data, d0, is instantiated in node n0)
			# nodes: n0

2. n0.send(d1) 	# toggle n0's downstream pointer to "Right"
			# on n0 there is no downstream node right so instantiate a new 
linked node right n1 with d1
			# nodes: n0,n1

3. n0. send(d2) # toggle n0's downstream pointer "Left"
			# in n0 there is no downstream node left so instantiate n2 with d2
			# nodes: n0,n1,n2

4. n0. send(d3) # toggle n0's downstream pointer "Right"
			# there exists a downstream node right, n1, so send d3 there
			# toggle n1's downstream pointer "Right"
			# in n1 there is no downstream node right so instantiate n3 with d3
			# nodes: n0,n1,n2,n3

5. n0. send(d4) # toggle n0's downstream pointer "Left"
			# there exists a downstream node left, n2, so send d4 there
			# toggle n2's downstream pointer "Right"
			# in n2 there is no downstream node right so instantiate n4 with d4
			# nodes: n0,n1,n2,n3,n4

6. n0.send(d5) 	# toggle n0's downstream pointer "Right"
			# there exists a downstream node right, n1, so send d5 there
			# toggle n1's downstream pointer "Left"
			# in n1 there is no downstream node left so instantiate n5 with d5
			# nodes: n0,n1,n2,n3,n4,n5

7. n0.send(d6)	# So on...
			# ...


Any thoughts about how to do this in Python appreciated. Possible at 
all? Thanks in advance,

-Rayme.



More information about the Pythonmac-SIG mailing list