Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82049
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <johnjposner@verizon.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.027 |
| X-Spam-Evidence | '*H*': 0.95; '*S*': 0.00; 'yet.': 0.04; 'def': 0.12; 'bug': 0.12; 'defaultdict': 0.16; 'dict': 0.16; 'key):': 0.16; 'received:206.46': 0.16; 'received:206.46.173': 0.16; 'self[key]': 0.16; 'subclass': 0.16; 'subject:key': 0.16; 'thread,': 0.16; 'fix': 0.17; "hasn't": 0.19; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'this:': 0.26; 'filed': 0.31; 'received:192.168.1.3': 0.31; 'class': 0.32; 'beginning': 0.33; 'skip:_ 10': 0.34; 'point.': 0.35; 'but': 0.35; 'subject:skip:d 10': 0.36; 'method': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'ago,': 0.61; 'received:206': 0.63; 'provide': 0.64; 'said:': 0.68; 'confusing': 0.84; 'surface': 0.84; 'subject:Setting': 0.96 |
| X-CMAE-Score | 0 |
| X-CMAE-Analysis | v=2.1 cv=AKpDk7pY c=1 sm=1 tr=0 a=pIVHBZShUZcs1nu43PzcsQ==:117 a=Tvs_GUuyyx4A:10 a=8nJEP1OIZ-IA:10 a=o1OHuDzbAAAA:8 a=oR5dmqMzAAAA:8 a=-9mUelKeXuEA:10 a=A92cGCtB03wA:10 a=uvxuWLzfZEBp435dOy0A:9 a=wPNLvfGTeEIA:10 |
| Date | Fri, 05 Dec 2014 15:51:55 -0500 |
| From | John J Posner <johnjposner@verizon.net> |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 |
| MIME-version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Setting default_factory of defaultdict to key |
| Content-type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-transfer-encoding | 7bit |
| X-Mailman-Approved-At | Sun, 07 Dec 2014 11:48:38 +0100 |
| 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.16662.1417949320.18130.python-list@python.org> (permalink) |
| Lines | 23 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417949320 news.xs4all.nl 2895 [2001:888:2000:d::a6]:43946 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:82049 |
Show key headers only | View raw
At the beginning of this thread, Ian Kelly said:
Not with defaultdict, but you can subclass dict and provide a
__missing__ method directly
To emphasize, you don't need to subclass "defaultdict" -- you need only subclass "dict" itself:
class MyDict(dict):
def __missing__(self, key):
self[key] = key
return key
md = MyDict()
md[1] = 111
_ = md[2]
_ = md["another key"]
## md now looks like this: {1: 111, 2: 2, 'another key': 'another key'}
The defaultdict documentation is confusing on this point. A *long* time ago, I filed Bug 9536 to improve the doc, but the fix hasn't bubbled to the surface yet.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Setting default_factory of defaultdict to key John J Posner <johnjposner@verizon.net> - 2014-12-05 15:51 -0500
csiph-web