Handling NameError in a list gracefully

J

Jesse Aldridge

from my_paths import *

def get_selected_paths():
return [home, desktop, project1, project2]

-------

So I have a function like this which returns a list containing a bunch
of variables. The real list has around 50 entries. Occasionally I'll
remove a variable from my_paths and cause get_selected_paths to throw
a NameError. For example, say I delete the "project1" variable from
my_paths; now I'll get a NameError when I call get_selected_paths.
So everything that depends on the get_selected_paths function is
crashing. I am wondering if there is an easy way to just ignore the
variable if it's not found. So, in the example case I would want to
somehow handle the exception in a way that I end up returning just
[home, desktop, project2].
Yes, I realize there are a number of ways to reimplement this, but I'm
wanting to get it working with minimal changes to the code. Any
suggestions?
 
C

Chris Rebert

from my_paths import *

def get_selected_paths():
   return [home, desktop, project1, project2]

-------

So I have a function like this which returns a list containing a bunch
of variables.  The real list has around 50 entries.  Occasionally I'll
remove a variable from my_paths and cause get_selected_paths to throw
a NameError.  For example, say I delete the "project1" variable from
my_paths; now I'll get a NameError when I call get_selected_paths.
So everything that depends on the get_selected_paths function is
crashing.  I am wondering if there is an easy way to just ignore the
variable if it's not found.  So, in the example case I would want to
somehow handle the exception in a way that I end up returning just
[home, desktop, project2].
Yes, I realize there are a number of ways to reimplement this, but I'm
wanting to get it working with minimal changes to the code.  Any
suggestions?

def get_selected_paths():
variables = "home desktop project1 project2".split()
vals = []
for var in variables:
try:
vals.append(getattr(my_paths, var))
except AttributeError:
pass
return vals
 
J

Jesse Aldridge

Nevermind, I figured it out right after I clicked the send button :\


from my_paths import *

def get_selected_paths():
return [globals() for s in
["home", "desktop", "project1", "project2"]
if s in globals()]
 
T

Terry Reedy

Jesse said:
from my_paths import *

def get_selected_paths():
return [home, desktop, project1, project2]

-------

So I have a function like this which returns a list containing a bunch
of variables. The real list has around 50 entries. Occasionally I'll
remove a variable from my_paths and cause get_selected_paths to throw
a NameError. For example, say I delete the "project1" variable from
my_paths; now I'll get a NameError when I call get_selected_paths.
So everything that depends on the get_selected_paths function is
crashing. I am wondering if there is an easy way to just ignore the
variable if it's not found. So, in the example case I would want to
somehow handle the exception in a way that I end up returning just
[home, desktop, project2].
Yes, I realize there are a number of ways to reimplement this, but I'm
wanting to get it working with minimal changes to the code. Any
suggestions?

I would consider giving my_paths a 'selected_paths' attribute that you
update by hand or automatically within my_paths.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top