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


Groups > comp.lang.python > #103139

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Herman <sorsorday@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date Thu, 18 Feb 2016 09:41:33 -0800
Lines 72
Message-ID <mailman.17.1455817335.2289.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de Zm0Sfpnn76kQRmYJr7FCYASFP6xy0ElXfkkmG117ZpFQ==
Return-Path <sorsorday@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'error:': 0.05; 'constructor': 0.07; 'override': 0.07; 'reason,': 0.07; 'subject:How': 0.09; 'dict': 0.09; 'key)': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'def': 0.13; '"we': 0.16; '...)': 0.16; 'byron': 0.16; 'defaultdict': 0.16; 'hierarchy': 0.16; 'inheritance': 0.16; 'iterable': 0.16; 'key):': 0.16; 'key:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:default': 0.16; '&gt;': 0.18; 'email addr:gmail.com&gt;': 0.18; 'steve': 0.18; 'thanks.': 0.18; 'suggested': 0.20; 'not,': 0.22; 'pass': 0.22; 'this:': 0.23; 'chris': 0.26; 'define': 0.27; 'message-id:@mail.gmail.com': 0.27; 'regular': 0.29; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'skip:d 40': 0.32; 'class': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:209': 0.38; 'received:209.85.220': 0.38; 'does': 0.39; 'subject:the': 0.39; 'from:': 0.39; 'to:addr:python.org': 0.40; 'save': 0.60; 'skip:\xc2 10': 0.67; 'skip:\xe2 10': 0.70; 'yourself': 0.73; '&quot;we': 0.84; '**kw)': 0.84; '2.7.': 0.84; '_o__)': 0.84; 'chrisa': 0.84; 'dare': 0.91; 'works!': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=6ygv9WQZK9Uc7yQjSyhK7hFyBzmNAvy/ASdRJJOcx88=; b=fRXUv1hcGlT9O1uv1ipvAxGwZiboIacsXh9Fx/R5x9JA2ptkZ5PrG4gqO4CWW7pG1o c4cGZUI7nWXEqnFeJsCS9IrKZBhvkx+zEW+PAK84DL7SO+EeWSn3BwqZmqLwX6zjMDay F8L+WzuItACKigWX4kxcboLE4wP6jGjzT/Kk0m7RdBHYKF4J2+1268gX6L3aWekiD2TN 0t3UmkbjlbZKCiLkZUALozGNQaInjKgBxgufJJEkYeanB/EBGTpjnor52CJhscVJOdan 20jeFuS/tdzLjhLuySafCA+7gNiJAW6jfoCxeakYF6ABICr9wzM69Hh3HqoCTQOD2X0K +wkg==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-type; bh=6ygv9WQZK9Uc7yQjSyhK7hFyBzmNAvy/ASdRJJOcx88=; b=gAUXPbSwTMBV4PtZmtDhSUld3u/xqgdTGP6fjAJhtbzrbmcKRuick5AJm8LXulVQuH QBFCrzKjLl3PdIbRoir56dTVvjeSeWspsq11d75F9xvwjN+jOLhpApiVVFsJcWSutoON RRUq5NzI8Dw6nDKBfRub2hGWPFohgIslQ84CrzMI2+9TpXXPrztYFx/400JMuG129dFt 9RSuy4GZSH2qdf8L68bEA85aHrAGN4dBeWBpw4B0i4ck/KGW3yysyhs8rsSS+tyvyM1m feG+yayD5v2DpBfWyivhsguzn/sfZ1wV8UziFiEhP0noUvCKqNRAhPSQ0hcO8YFmQ0de MSQg==
X-Gm-Message-State AG10YOSi+k+CVLH0YpjfUpVi9OXEn1DUsrszt/kgpYh1fmtRmynSG3A7AYFDTpUPDvLGRQv9/MlS8qU4vrHjAw==
X-Received by 10.55.71.66 with SMTP id u63mr10224940qka.67.1455817332858; Thu, 18 Feb 2016 09:42:12 -0800 (PST)
X-Content-Filtered-By Mailman/MimeDel 2.1.21rc2
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:103139

Show key headers only | View raw


d = dictutil.DefaultDictWithEnhancedFactory(lambda k: k)
        self.assertEqual("apple", d['apple'])

From: Ben Finney <ben+python@benfinney.id.au>
>
> you are using the inheritance hierarchy but thwarting it by not using
> ‘super’. Instead::
>
>     super().__init__(self, default_factory, *a, **kw)
>
> and::
>
>     super().__getitem__(self, key)
> --
>  \       "Those who will not reason, are bigots, those who cannot, are |
>   `\        fools, and those who dare not, are slaves." —“Lord” George |
> _o__)                                                Gordon Noel Byron |
> Ben Finney

super does not work for defaultdict. I am using python 2.7. If I use
super(defaultdict, self).__init__(default_factory, *a, **kw), I get the
error:

    super(defaultdict, self).__init__(default_factory, *a, **kw)
TypeError: 'function' object is not iterable

My use case is:
d = dictutil.DefaultDictWithEnhancedFactory(lambda k: k)
        self.assertEqual("apple", d['apple'])


> From: Chris Angelico <rosuav@gmail.com>
>
> Save yourself a lot of trouble, and just override __missing__:
>
> class DefaultDictWithEnhancedFactory(collections.defaultdict):
>     def __missing__(self, key):
>         return self.default_factory(key)
>
> ChrisA
>
This works! Thanks.

From: "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
> > I want to pass in the key to the default_factory of defaultdict
>
> Just use a regular dict and define __missing__:
>
> class MyDefaultDict(dict):
>     def __missing__(self, key):
>         return "We gotcha key %r right here!" % key
>
>
> If you want a per-instance __missing__, do something like this:
>
>
> class MyDefaultDict(dict):
>     def __init__(self, factory):
>         self._factory = factory
>     def __missing__(self, key):
>         return self._factory(self, key)
>
>
> d = MyDefaultDict(lambda self, key: ...)
>
>
> --
> Steve
>
Look like inheriting from defaultdict is easier. I don't even  have to
override the constructor as suggested by Chris Angelico above. Thanks.

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


Thread

Re: How to properly override the default factory of defaultdict? Herman <sorsorday@gmail.com> - 2016-02-18 09:41 -0800

csiph-web