At 08:38 PM 11/21/2005, Vincent Wan wrote:
>I'm trying to write a tree data structure as part of my first
>object oriented program
>
>I have an error "can't assign to function call" caused by this line:
>Tree.nodeList(self.name) = self
Tree.nodeList[self.name] = self
>however, nodeList is a dictionary I'm trying to index.
>
>Beyond the error I'm still not sure I understand how to make and
>use a tree data structure using objects.
>
>Thank you for the help
>
>Here is my code
>
># obj_tree1.py
>
>import random
>
># constants that control the simulation
>NUMBER_REPS = 10 # run's the simulation
>MAX_LINAGES = 10 # number of species in each run
>BRANCHING_PROBABILITY = 0.5
>
>class Tree(object):
> numLinages = 0 # keeps track of how many nodes there are
> nodeList = {} # keeps track of the nodes
> class Node(object):
> def __init__(self, name):
> self.name = name # an integer
> self.alive = True
> self.descendents = {} # nodes descending from self
> Tree.numLinages += 1 # records node creation
> Tree.nodeList(self.name) = self # makes node
>accesable from tree
> def __init__(self):
> nodeList(0) = Tree.Node(0) # adds a root node 0 to the tree
> def AddBranch(self, offspring):
> self.descendents(offspring) = Tree.Node(offspring) # adds
>a descendent node
> def NumLinages( ):
> return Tree.numLinages
> NumLinages = staticmethod(NumLinages)
>
>currentTree = Tree()
>
>for i in range(NUMBER_REPS):
> j = 0
> while j <= currentTree.NumLinages(): # checks all node because
>their names are sequential integers
> if j.alive:
> if random.random() >= BRANCHING_PROBABILITY:
> currentTree.AddBranch(j, (currentTree.NumLinages() +
>1)) # creates a new node
> j += 1
>
>
>Vincent Wan
>
>------------------------------------------------------------------------
>--------------
>PhD Candidate
>Committee on the Conceptual and Historical Studies of Science
>University of Chicago
>
>PO Box 73727
>Fairbanks, AK 99707
>
>wan AT walrus DOT us (change CAPS to @ and . )
>
>_______________________________________________
>Tutor maillist - [email protected]
>http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor