Classes and functions.

Terry Reedy tjreedy at udel.edu
Fri Sep 26 17:14:39 EDT 2008


aditya shukla wrote:
> Hello folks , i am using the newick module 
> http://www.daimi.au.dk/~mailund/newick.html.I am just learning to use it 
> and i have a question about it.
> 
> from  newick.tree import parse_tree
> from  newick.tree import add_parent_links
> from  newick.tree import add_distance_from_root
> 
> import sys
> 
> t = parse_tree('((A:2,B:3):1,C:6);')
> 
> print t
> 
> deltas = add_distance_from_root(t)
> 
> now when i do this i can get the output like this
> 
> 
> (('A' : 2.0, 'B' : 3.0) : 1.0, 'C' : 6.0)
> None
> 
> This is the code of the add_distance_from_root(0
> 
> def add_distance_from_root(tree):
>     '''Extend all nodes with the distance (branch length) from the root'''
>     tree.distance_from_root = 0.0       # 'tree' is the root...
>    
>     class V(TreeVisitor):
>         def pre_visit_edge(self,src,b,l,dst):
>             if l is None: l = 0
>             dst.distance_from_root = src.distance_from_root - l

I am puzzled why this class is inside the function and why -1 instead of 
+1, but...

>     tree.dfs_traverse(V())
>     
>  From here it is clear that the function does not return anything but i 
> wanna get the value of the distance from root.How can i get this?

I presume each node gets an attribute 'distance_from_root', so read that 
  attribute of whatever node.




More information about the Python-list mailing list