Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82066 > unrolled thread
| Started by | Dave Angel <davea@davea.name> |
|---|---|
| First post | 2014-12-07 11:31 -0500 |
| Last post | 2014-12-07 11:31 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Setting default_factory of defaultdict to key Dave Angel <davea@davea.name> - 2014-12-07 11:31 -0500
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-12-07 11:31 -0500 |
| Subject | Re: Setting default_factory of defaultdict to key |
| Message-ID | <mailman.16676.1417969885.18130.python-list@python.org> |
On 12/05/2014 03:51 PM, John J Posner wrote:
> At the beginning of this thread, Ian Kelly said:
Since this clearly is intended to be part of the earlier thread, please
make it so by using reply-list or whatever equivalent your email program
has.
>
> 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.
>
>
--
DaveA
Back to top | Article view | comp.lang.python
csiph-web