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?

Path csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Gregory Ewing <greg.ewing@canterbury.ac.nz>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date Mon, 15 Feb 2016 21:28:28 +1300
Lines 16
Message-ID <didgheFteu2U1@mid.individual.net> (permalink)
References <mailman.122.1455495511.22075.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net N+HQPdyFTeD8scnMe2dkzAEC9X/zNr4SKwrbbLR0Fxo9UxAWaB
Cancel-Lock sha1:0JXYN+Ly/eVpCpgdaGV0kdiLRdo=
User-Agent Mozilla Thunderbird 1.0.5 (Macintosh/20050711)
X-Accept-Language en-us, en
In-Reply-To <mailman.122.1455495511.22075.python-list@python.org>
Xref csiph.com comp.lang.python:102953

Show key headers only | 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