Is it possible to get the erroneous variable when getting a NameError exception?

Dotan Barak dotanba at gmail.com
Fri Dec 25 10:29:48 EST 2009


Hi.

I'm trying to evaluate a string and getting a NameError
(it is expected, since the variable my_number wasn't provided in the 
"locals" dictionary).

<--snip start-->
 >>> eval("my_number < 10", {"__builtins__":None}, {})
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<string>", line 0, in ?
NameError: name 'my_number' is not defined
<--snip end-->

My question is: how can i know which variable name / symbol causes the 
NameError exception?
In my example, this symbol is my_number.

Using that information, I will be able to print a nice error message to 
the user.


Thanks
Dotan





More information about the Python-list mailing list