--- On Sat, 4/24/10, James Mills <prolo...@shortcircuit.net.au> wrote:
> From: James Mills <prolo...@shortcircuit.net.au> > Subject: Re: NameError: how to get the name? > To: "Yingjie Lan" <lany...@yahoo.com> > Cc: "python list" <python-list@python.org> > Date: Saturday, April 24, 2010, 4:03 PM > On Sat, Apr 24, 2010 at 9:19 PM, > Yingjie Lan <lany...@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 -- http://mail.python.org/mailman/listinfo/python-list