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


Groups > comp.lang.python > #102930

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date Mon, 15 Feb 2016 12:04:20 +1100
Lines 13
Message-ID <mailman.125.1455498268.22075.python-list@python.org> (permalink)
References <CANOe_mhsAmLUYJ+7VfHWuOjKJwm=T59OVLyLRfasCYpqOAV-ZA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de QWqVCPY6WDfU4hjZDPa5rQNGpFe3mwf8Lg2R/MOmn02g==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'override': 0.07; 'cc:addr :python-list': 0.09; 'subject:How': 0.09; 'def': 0.13; '2016': 0.16; 'defaultdict': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'herman': 0.16; 'key):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:default': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'pass': 0.22; 'am,': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; 'code:': 0.29; '15,': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'instead': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'save': 0.60; 'yourself': 0.73; 'chrisa': 0.84; "class's": 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dXmsEPJ8MfX0rcy6uvmLS30cdcZ9ci2sag/vJ1VZhrM=; b=yR+jlxkZ2QyJHp3/P86WQD2BQoPFd1KGeUAwVEOU96H5O6WV/4A9L9QBKIK0kedSUi 2Z5twInHOtbtrO2qzcGDhpx0mGaDMU3cc2/x3wMAtUUHvleWc5wyWU+RchuHHcvpCU8c tAlqyxPu3BbpoKrWDtombOOM12l6h3QI/FCDLYEa8mfMqvouzNZVtPrSd5tsWMZ1eNHg NZ0bJELQuSUIQ5H6YqRP+IngYP8mlOzShHh3p7qgHITOCrWljyRUyKOjh6660HBzVQV4 0dFq68riCLrSDM6zhd65GQPkSpt2PjQ1iPj7aseS5x4nFGxPiccS7GH26GvN+DuizJU3 hamw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=dXmsEPJ8MfX0rcy6uvmLS30cdcZ9ci2sag/vJ1VZhrM=; b=UAi6d9aboBXUMRz4x29UWxBUey1B7B+W52sqMYgpsOUYO7+FR+39r17+66p4SAedSI 6MELii6jr6DeGSGpk4oL8TUgp+qEKMo0H6s6g7iidiRUGOt1yqWk7TNvIXm7QRUn+SDm 8a9SaIkoZajqaDLl7B/vpNNIkyEfD5A3fw81N4BfLyBkiFC75VUBPpGnGXD3RbkYbPtO MHkGeZ/obG1auTGIE5Fcrc7H5ax6TFwlmy14uAcIg5XvLdbvSAPBczZKgeiXoHDcogR3 2KknWxyCFi7IzemSP7GIUYqVbbs0mUd+xm0bJfuTxIQFdSGYHcvqK/atHrxpSzXQDjkT tNZA==
X-Gm-Message-State AG10YORriMrcwmKnkQbqnPRc72p2EjbOBuNuIwAXzTCF4GeHJEQV4hewFRQhfki8MrDahabu+VHxQ82v3YgOwQ==
X-Received by 10.50.28.105 with SMTP id a9mr10171308igh.94.1455498260113; Sun, 14 Feb 2016 17:04:20 -0800 (PST)
In-Reply-To <CANOe_mhsAmLUYJ+7VfHWuOjKJwm=T59OVLyLRfasCYpqOAV-ZA@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102930

Show key headers only | View raw


On Mon, Feb 15, 2016 at 11:17 AM, Herman <sorsorday@gmail.com> 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), so my class's __getitem__ have to catch a TypeError instead instead
> of KeyError. The following class is my code:

Save yourself a lot of trouble, and just override __missing__:

class DefaultDictWithEnhancedFactory(collections.defaultdict):
    def __missing__(self, key):
        return self.default_factory(key)

ChrisA

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


Thread

Re: How to properly override the default factory of defaultdict? Chris Angelico <rosuav@gmail.com> - 2016-02-15 12:04 +1100

csiph-web