What do I do when I need a self referential dictionary?

No fear of creating new classes –
You can take advantage of Python’s string formating capabilities
and simply do:

class MyDict(dict):
   def __getitem__(self, item):
       return dict.__getitem__(self, item) % self

dictionary = MyDict({

    'user' : 'gnucom',
    'home' : '/home/%(user)s',
    'bin' : '%(home)s/bin' 
})


print dictionary["home"]
print dictionary["bin"]

Leave a Comment