From: Yingjie Lan on
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?

Thanks in advance,

Yingjie



From: James Mills on
On Sat, Apr 24, 2010 at 9:19 PM, Yingjie Lan <lanyjie(a)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
From: Steven D'Aprano on
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:

def test():
x = 1
print(x)
locals()['x'] = 2
print(x)

A better approach would be:

try:
b
except NameError:
b = ''
# and the same for c

def fun(a, b=b, c=c):
pass


Better still, just make sure b and c are defined before you try to use
them!



> 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?


'name' in locals()



--
Steven
From: Yingjie Lan on
--- On Sat, 4/24/10, Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au> wrote:

> From: Steven D'Aprano <steve(a)REMOVE-THIS-cybersource.com.au>
> Subject: Re: NameError: how to get the name?
> To: python-list(a)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
>>>




From: Yingjie Lan on
--- On Sat, 4/24/10, James Mills <prologic(a)shortcircuit.net.au> wrote:

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