I have a set of classes that have as one of their attributes a url. I would like to build a dictionary of those classes, keyed by that url. Here is the code I have come up with:
class pagesByUrl(dict):
"a cross reference of pages by url rather than object name"
def __init__(self):
pages={}
for page in dir(xxxPages):
try:
pgAttr=getattr(xxxPages, page)
pg=pgAttr('dummybrowser')
pages[pg.url] = page
except (KeyError, TypeError, AttributeError):
pass
print pages #At this point, the dictionary is good.
self=pages
print self #Also here, still just what I want.
pg=pagesByUrl()
print "pg is:", pg #But here, pg is an empty dictionary.
What can I do to have this class instantiate as the dictionary that I want?