exec/eval changed?

Michael Hudson mwh21 at cam.ac.uk
Tue Nov 16 06:16:12 EST 1999

JB <jimbag at kw.igs.net> writes:

> Michael Hudson wrote:
> > 
> > JB <jimbag at kw.igs.net> writes:
> > 
> > > Did the exec/eval statements change somehow in the 1.5 versions? I
> > > have a script that did an eval( "some code", dict1, locals()) but
> > > now it doesn't work. I've looked through the doc's but they are
> > > little thin as to these statements. Any help is appreciated.
> > 
> > Well, eval("some code") is a syntax error. What are you trying to to
> > do? We can't read minds here.
> > 
> > Regards,
> > Michael
> My apologies. I was thinking that the scoping for the 2nd and 3rd
> parameters to the eval call had changed somehow and thus the code was
> irrelevant to the example.
> What I am trying to do is simple eval a string with python formatting
> operations within and return the result. The code string is dynamic.
> Basically: 
> eval( "'The value of t is %(t)s' % t", self.private_dict, locals())
> where t is in self.private_dict. The questions comes because this code
> worked in python v1.5 but does not work in v 1.5.2 and I was curious if
> there had been any changes to the scoping or to the eval/exec calls
> themselves.

How does it fail? If I try something like that I get:

Traceback (innermost last):
  File "<pyshell#3>", line 1, in ?
  File "<pyshell#2>", line 6, in go
    return eval( "'The value of t is %(t)s' % t", self.private_dict, locals())
  File "<string>", line 0, in ?
TypeError: format requires a mapping

If I change it to

return eval( "'The value of t is %s' % t", self.private_dict, locals())

then it works.

You aren't trying to do 

return 'The value of t is %(t)s'%self.private_dict

are you? The use of eval seems a bit overkill here!

> Sorry for the confusion.

That's OK; but for future reference you might want to include how your
problem code fails as well!


More information about the Python-list mailing list