Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81760 > unrolled thread

Setting default_factory of defaultdict to key

Started byLarry Martell <larry.martell@gmail.com>
First post2014-12-01 13:05 -0500
Last post2014-12-01 13:05 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Setting default_factory of defaultdict to key Larry Martell <larry.martell@gmail.com> - 2014-12-01 13:05 -0500

#81760 — Setting default_factory of defaultdict to key

FromLarry Martell <larry.martell@gmail.com>
Date2014-12-01 13:05 -0500
SubjectSetting default_factory of defaultdict to key
Message-ID<mailman.16457.1417457107.18130.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web