Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80183

Re: Dictionaries with variable default.

From Cousin Stanley <cousinstanley@gmail.com>
Newsgroups comp.lang.python
Subject Re: Dictionaries with variable default.
Followup-To comp.lang.python
Date 2014-11-04 07:13 -0700
Organization Hohum Acres
Message-ID <m3amur$1d6$1@dont-email.me> (permalink)
References <54576150.6090002@rece.vub.ac.be> <CAPTjJmo451DeTNhJnQQNaWJPWMDeBvMoiKK=DQidqKDVedxDLw@mail.gmail.com> <mailman.15444.1415107297.18130.python-list@python.org>

Followups directed to: comp.lang.python

Show all headers | View raw


> 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

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Dictionaries with variable default. Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-11-04 14:21 +0100
  Re: Dictionaries with variable default. Cousin Stanley <cousinstanley@gmail.com> - 2014-11-04 07:13 -0700

csiph-web