NameError: how to get the name?

Yingjie Lan lanyjie at yahoo.com
Sat Apr 24 16:43:27 CEST 2010


--- On Sat, 4/24/10, James Mills <prologic at shortcircuit.net.au> wrote:

> From: James Mills <prologic at shortcircuit.net.au>
> Subject: Re: NameError: how to get the name?
> To: "Yingjie Lan" <lanyjie at yahoo.com>
> Cc: "python list" <python-list at python.org>
> Date: Saturday, April 24, 2010, 4:03 PM
> 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"
> 

Thanks for the info, that's a little messy to play with though.

Yingjie


      



More information about the Python-list mailing list