Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80183
| 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 <cousinstanley@gmail.com> |
| 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 | <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> |
| 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 |
Followups directed to: comp.lang.python
Show key headers only | 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
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