self
Vojin Jovanovic
vjovanov at stevens-tech.edu
Fri Jun 7 10:51:17 EDT 2002
> Why such a
> harsh reaction without bothering to correct the obvious 'typo'?
No, it is not the 'typo'. I corrected that immediately.
Also you had in your message >>> e=Evaluator({'a':'3']), a wrong bracket.
Go back to your post and check it out. That is why I thought that you
didn't test it.
However, all that aside, the problems is that your code just din't work
after I corrected those 2 'typos'. Take a look yourself ....
Python 2.1.2 (#31, Jan 15 2002, 17:28:11) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class Evaluator:
def __init__(self, eqs={}):
self.__dict__['equations']=eqs
def __getattr__(self, name):
value = self.equations[name]
if type(value)!=str:
return value # if it's not a string, just give it back
scope={}
while 1:
try:
return eval(value, scope)
except NameError, n:
var=str(n).split("'")[1]
scope[var]=getattr(self, var)
def __setattr__(self, name, value):
self.equations[name]=value
>>> class Berta:
a=5
>>> e=Evaluator({'a':3})
>>> e.berta=Berta()
>>> e.g='berta.a*a'
>>> e.g
'berta.a*a'
>>>
V.J.
More information about the Python-list
mailing list