?Method to list out map object's keys on KeyError

Norman Shelley Norman.Shelley at motorola.com
Fri Oct 10 19:04:41 EDT 2003


Is there a way to get at the object one is trying to access with a key to list
out all the possible keys?
See the except clause below.

#!/usr/bin/env python2.2
from inspect import *
import sys

program = """
abc = {}
print "abc:", abc
print abc['def']
"""


gdct = globals()
ldct = {}
try:
 exec program in gdct, ldct
except KeyError:
 # Some way to get at the abc object to list out all its keys
 # e.g. abc.keys()  Of course program could have any map name
 # that would fail and I would like that object's keys
 raise







More information about the Python-list mailing list