Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Gregory Ewing 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: References: 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: Xref: csiph.com comp.lang.python:102953 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