self

Vojin Jovanovic vjovanov at stevens-tech.edu
Wed Jun 5 14:40:45 EDT 2002


> no his questions go deeper :-)

I am glad that somebody here recognizes that.

> For every name in such a value you need to evaluate its value again.
> IOW it's recursive. The obvious solution is to intercept the
namevalue-lookups
> during evaluation of an equation. But this doesn't work for exec/eval:
> They ignore the interception points.

Correct.

Every evaluation has to go through __getattr__ method in order to access
the instance variables of the object, but that, unfortunately, can only be
done with self.
This is needed since the left hand sides of the equations must be the
variables of the instance
and not just some key values in a dictionary.

> The next best solution IMO is to preprend a namescope like 'self.'
> to every name in an expression.
> But you may have to parse the expression to
> do this.

I have been thinking about this for some time, and realized that there some
tricky cases that one has to deal with, and I am not sure that the whole
business of appending
self. will be error free in every case. For example in 'x + Geometry.x' the
correct result should
be 'self.x + self.Geometry.x' where Geometry is an instance of Geometry
class that is bound to
self.Geometry.  So one has to distinguish between two names of x in two
different places.  Maybe
stuff like this is not even so tricky.  I don't know at this point, I
haven't looked at that too
closely yet to decide the feasibility of such an approach.  I am still
trying to resolve
the problem in an easier way with whatever Python is offering to me.

> Maybe fixing exec/eval behaviour is a better idea?

Well, I don't know how to proceed in this regard. I am not familiar with the
source code.

It seems that the only two options are 1) work directly on the input string
or 2) parse the expression
and try to correctly append self. in front of the variables.

V. Jovanovic





More information about the Python-list mailing list