string formatting with missing dictionary key

Jeff Epler jepler at unpythonic.net
Mon Jan 13 14:20:31 EST 2003


(Requires Python 2.2)

class D(dict):
    def __getitem__(self, item):
        if not self.has_key(item): return "default"
        return super(D, self).__getitem__(item)
        
print "%(a)s ; %(d)s; %(e)s" % D({'a':1, 'b':2, 'c':3})





More information about the Python-list mailing list