self

Fernando Pérez fperez528 at yahoo.com
Wed Jun 5 10:28:41 EDT 2002


Vojin Jovanovic wrote:

>> 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.

You seem to be trying to set up a system whose evaluation rules are very 
similar to those in Mathematica. I've long toyed with this idea myself, as an 
addition I'd love to make to IPython.  What I've had in mind is allowing a 
new definition symbol in the IPython prompt ':=' which like in Mathematica 
represents 'delayed evaluation'. This part would be trivial, as the hooks for 
special syntax processing are already in place (an example for physical 
quantities with units is provided and one for pasting code from python 
prompts). You'd then have something like (imagine this is at an interactive 
prompt):

y=5  # normal variable
x:=y+1   # x is a delayed evaluation variable, stored in a special class
x # prints 6 on screen
y=9
x  #prints 10 on screen

So ':=' variables would first try to evaluate their expressions in their 
normal namespace (that of the class, the problem you're dealing with now) and 
would then look for 'normal' variable names to resolve. Only if a name 
wasnt't found in either would a NameError be raised.

Unfortunately I don't have the time right now to work on this (and won't for a 
while). But if you want to add the syntax processor and the special class 
using IPython (which gives you finer control over the namespaces wherer 
things are evaluated than you get with the normal interpreter) I'll be glad 
to include it in future releases.

Cheers,

f.

ps. IPython is at http://www-hep.colorado.edu/~fperez/ipython/



More information about the Python-list mailing list