Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cousin Stanley Newsgroups: comp.lang.python Subject: Re: Dictionaries with variable default. Followup-To: comp.lang.python Date: Tue, 04 Nov 2014 07:13:54 -0700 Organization: Hohum Acres Lines: 34 Message-ID: References: <54576150.6090002@rece.vub.ac.be> Reply-To: cousinstanley@gmail.com Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit Injection-Date: Tue, 4 Nov 2014 14:13:47 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="395019222570aa09efc17e1724d4463a"; logging-data="1446"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fQw3SBjyan3C9cnNys5vXnpGjm+jOhlw=" User-Agent: KNode/4.4.11 Cancel-Lock: sha1:OA2xFI4BbeAv6NQnVOSAZATYY9w= Xref: csiph.com comp.lang.python:80183 > So How should I call this: > > class ...dict(dict): > def __init__(self, fun): > self.fun = fun > > def __missing__(self, key): > return self.fun(key) I don't know how you should, but I tried the following which seems to work .... class KeyPlusOne( dict ) : def __missing__( self , key ) : return ( 2 * key ) + 1 kp1 = KeyPlusOne() d = { } for n in range( 11 ) : d[ n ] = kp1[ n ] print '\n key : value \n' for key , value in d.iteritems() : print ' %2d : %2d' % ( key , value ) -- Stanley C. Kitching Human Being Phoenix, Arizona