Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81762 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2014-12-01 11:11 -0700 |
| Last post | 2014-12-01 11:11 -0700 |
| 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 Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-01 11:11 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-12-01 11:11 -0700 |
| Subject | Re: Setting default_factory of defaultdict to key |
| Message-ID | <mailman.16458.1417457926.18130.python-list@python.org> |
On Mon, Dec 1, 2014 at 11:05 AM, Larry Martell <larry.martell@gmail.com> wrote: > Is there a way to set the default_factory of defaultdict so that > accesses to undefined keys get to set to the key? > > i.e. if d['xxx'] were accessed and there was no key 'xxx' then > d['xxx'] would get set to 'xxx' > > I know I can define a function with lambda for the default_factory, > but I don't see how to access the key. I tried: > > d = defaultdict(lambda: key) > > But that did not work. Not with defaultdict, but you can subclass dict and provide a __missing__ method directly, which is passed the key. This is basically all that defaultdict does.
Back to top | Article view | comp.lang.python
csiph-web