[Tutor] btree problems

Justin Sheehy dworkin@ccs.neu.edu
15 Mar 2000 15:11:52 -0500

Bruce Sass <bsass@freenet.edmonton.ab.ca> writes:

> 	def Insert(self, newnode, branch=self.tree)
> 	...
> but it fails, complaining about the "self" portion of the "self.tree"
> object... Why can't the compiler see the self.tree in the __init__ a
> few lines previous?

Default parameters are computed when the function definition is
evaluated.  As this is before the object is instantiated, self.tree
has not yet been created.

> Am I being silly, expecting the compiler to look inside __init__?
> After all, if __init__ exists, it will be run first thing whenever an
> instance of a class is created, so it should be valid to reference stuff
> in __init__ when defining methods of the class, right.
> Is there a technical reason for the compiler not looking inside
> __init__, or was it just an oversight that may be fixed in future
> versions of Python?

Since you can subclass a class and then override methods, one cannot
guarantee that all of the methods of a given object will always be the
same as when a given class was originally defined.  

It would be a drawback if the compiler were to treat __init__
specially in this regard.