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


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

Re: Setting default_factory of defaultdict to key

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2014-12-01 11:35 -0700
Last post2014-12-01 11:35 -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.


Contents

  Re: Setting default_factory of defaultdict to key Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-01 11:35 -0700

#81769 — Re: Setting default_factory of defaultdict to key

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-12-01 11:35 -0700
SubjectRe: Setting default_factory of defaultdict to key
Message-ID<mailman.16465.1417459396.18130.python-list@python.org>
On Mon, Dec 1, 2014 at 11:29 AM, Larry Martell <larry.martell@gmail.com> wrote:
> I spoke too soon:
>
>>>> class defaultdictkey(defaultdict):
> ...  def __missing__(self, key):
> ...   self[key] = self.default_factory(key)
> ...
>>>> x = defaultdictkey(lambda k: k)
>>>> print x['aaa']
> None
>>>> print x['aaa']
> aaa
>>>> a = x['qqq']
>>>> print a
> None

You need to also return the value from __missing__.

[toc] | [standalone]


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


csiph-web