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


Groups > comp.lang.python > #102953

Re: How to properly override the default factory of defaultdict?

From Gregory Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date 2016-02-15 21:28 +1300
Message-ID <didgheFteu2U1@mid.individual.net> (permalink)
References <mailman.122.1455495511.22075.python-list@python.org>

Show all headers | View raw


Herman wrote:
> I want to pass in the key to the default_factory of defaultdict and I found
> that defaultdict somehow can intercept my call to dict.__getitem__(self,
> key),

What's happening here is that defaultdict doesn't actually
override __getitem__ at all. Instead, it overrides __missing__,
which gets called by the standard dict's __getitem__ for a
missing key.

As Steven said, you don't need a defaultdict here at all,
just a dict subclass that defines __missing__ the way you
want.

-- 
Greg

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to properly override the default factory of defaultdict? Herman <sorsorday@gmail.com> - 2016-02-14 16:17 -0800
  Re: How to properly override the default factory of defaultdict? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-15 14:56 +1100
  Re: How to properly override the default factory of defaultdict? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-15 21:28 +1300

csiph-web