self
holger krekel
pyth at devel.trillke.net
Thu Jun 6 20:30:10 EDT 2002
Vojin Jovanovic wrote:
> I don't think you tested your code, because I can't get the output you
> specified. In fact it seems that you broke your own code.
of course i tested it! Do you really think i am making up
results which can be proven wrong in ten seconds? Why such a
harsh reaction without bothering to correct the obvious 'typo'?
I don't actually know what went wrong while pasting the
code ('Namerror' instead of 'NameError').
Anyway, here is the paste-typo-corrected version:
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
15
regards,
holger
More information about the Python-list
mailing list