Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81763
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ethan@stoneleaf.us> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'modify': 0.07; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'undefined': 0.09; '~ethan~': 0.09; 'def': 0.12; '10:05': 0.16; 'accesses': 0.16; 'defaultdict': 0.16; 'filename:fname piece:signature': 0.16; 'key):': 0.16; 'key?': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; '{})': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'pass': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'along': 0.30; 'keys': 0.31; 'larry': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'there': 0.35; 'subject:skip:d 10': 0.36; 'charset:us- ascii': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:173': 0.61; 'subject:Setting': 0.96 |
| Date | Mon, 01 Dec 2014 10:19:24 -0800 |
| From | Ethan Furman <ethan@stoneleaf.us> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Setting default_factory of defaultdict to key |
| References | <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> |
| In-Reply-To | <CACwCsY7sCi51Aq9z3vbSohG-gq-xHyo_=jXzS04FUVy1b8F5Qw@mail.gmail.com> |
| Content-Type | multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GaXx45WdU89inC4uvdNlRijJTAAKhhLAn" |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16459.1417457972.18130.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417457972 news.xs4all.nl 2867 [2001:888:2000:d::a6]:42235 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81763 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 12/01/2014 10:05 AM, Larry Martell wrote:
>
> Is there a way to set the default_factory of defaultdict so that
> accesses to undefined keys get to set to the key?
You need to subclass and modify __missing__ to actually pass along the key:
--> class defaultdictkey(defaultdict):
... def __missing__(self, key):
... self[key] = self.default_factory(key)
... return self[key]
...
and in action:
--> huh = defaultdictkey(lambda k: k)
--> huh
defaultdict(<function <lambda> at 0x7fe1305de3f0>, {})
--> huh['x']
'x'
--> huh['x']
'x'
--> huh.get('y')
--> huh['y']
'y'
--> huh.get('y')
'y'
--
~Ethan~
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Setting default_factory of defaultdict to key Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 10:19 -0800
csiph-web