NameError: how to get the name?
James Mills
prologic at shortcircuit.net.au
Sat Apr 24 08:03:50 EDT 2010
On Sat, Apr 24, 2010 at 9:19 PM, Yingjie Lan <lanyjie at yahoo.com> wrote:
> I wanted to do something like this:
>
> while True:
> try:
> def fun(a, b=b, c=c): pass
> except NameError as ne:
> name = get_the_var_name(ne)
> locals()[name] = ''
> else: break
>
> What's be best way to implement the function
> get_the_var_name(ne) that returns the name
> of the variable that could not be found?
A NameError Exception does not store the name of the
variable. it has two attributes: .args and .message
both of which contain (usually) a string such as:
"name 'x' is not defined"
cheers
James
More information about the Python-list
mailing list