--- On Sat, 4/24/10, Steven D'Aprano <st...@remove-this-cybersource.com.au> wrote:
> From: Steven D'Aprano <st...@remove-this-cybersource.com.au> > Subject: Re: NameError: how to get the name? > To: python-list@python.org > Date: Saturday, April 24, 2010, 4:07 PM > On Sat, 24 Apr 2010 04:19:43 -0700, > Yingjie Lan 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 > > This won't work. Writing to locals() does not actually > change the local > variables. Try it inside a function, and you will see it > doesn't work: > I tried this, and it worked: Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> while True: ... try: print a ... except: locals()['a']="HERE YOU ARE" ... else: break ... HERE YOU ARE >>> -- http://mail.python.org/mailman/listinfo/python-list