Changing self: if self is a tree how to set to a different self

Kay Schluehr kay.schluehr at
Sat Jul 12 15:35:56 CEST 2008

On 10 Jul., 15:19, Bart Kastermans <bkast... at> wrote:
> I am playing with some trees.  In one of the procedures I wrote
> for this I am trying to change self to a different tree.  A tree
> here has four members (val/type/left/right).  I found that self = SS
> does not work; I have to write self.val = SS.val and the same for
> the other members (as shown below).  Is there a better way to do this?
> In the below self is part of a parse tree, F is the parse tree of a
> function f with argument x.  If a node in the parse tree is labelled
> f, we should replace it by the parse tree for the function f, F, with
> the remainder of the tree substituted for the input variable for the
> function f, here x.
>     def elimF (self):
>         if self.val == "f":
>             SS = F.copy ()
>             SS.subst ('x', self.left)
>             self.val = SS.val        # from here: set self to be SS
>             self.type = SS.type
>             self.left = SS.left
>             self.right = SS.right    # completed: set self to be SS
>         if self.left != None:        # iterate onward, inf recursion if f
>                                      # appears.  Would need a check in
>                                      # real version
>             self.left.elimF ()
>         if self.right != None:
>             self.right.elimF ()
> Best,
> Bart

Since it is acting on a tree why doesn't the code substitute self in
its parent by SS? That's the natural perspective if one considers a
tree as a linked structure and inserts and deletes nodes within this

More information about the Python-list mailing list