extending auto_dict

R W randall at ymail.com
Wed Jul 2 03:03:34 EDT 2008


I'm using the excellent 'auto_dict' (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/537637) but I'd like to be able to extend it.

Currently my program has

class auto_dict(dict):
    def __getitem__(self, key):
        return self.setdefault(key, self.__class__())

daily=auto_dict()
try:
    daily['today']['now'].append((1,2))
except AttributeError:
    daily['today']['now']=[(1,2)]


Which works OK but I thought it would be easy to supply an 'append' method to the auto_dict class that would save me from having all those try/except blocks scattered through my code ie I could just say

daily['today']['now'].append((1,2))

and the class would take care of catching the exception and assigning the list.

I tried adding

def append(self,item):
       self=[item]
       return self

to the class definition but it doesnt actually assign the value.  

Can it be done this way?  Some other way?  Or am I stuck using the try/except blocks?
 
    

(please CC me since I'm not subscribed to the list)


      Get the name you always wanted with the new y7mail email address.
www.yahoo7.com.au/mail
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080702/2a6bbde2/attachment.html>


More information about the Python-list mailing list