eval function not working how i want it dag namn

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Apr 19 22:24:56 EDT 2005


robcarlton wrote:
> thanks. I'll use the getattr function now, and I think I understand
> where I went wrong with eval. I was thinking in Lisp where the lexical
> scope would mean that obj is defined

The full story is actually more subtle. The name 'obj'
*is* accessible from a nested scope if you do something
like

   def f(obj):
     def g():
       print obj
     g()

But the bytecode compiler has to do extra work to make
a name in an intermediate scope accessible to an inner
scope, and it only does this if it sees a reference to
the name in the inner scope. In your code, the reference
is invisible at compile time, so the compiler misses it,
and the run-time evaluation fails.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list