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


Groups > comp.lang.python > #78470

Re: trouble building data structure

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'exists.': 0.07; 'try:': 0.09; 'def': 0.12; 'itself.': 0.14; 'arbitrarily': 0.16; 'defaultdict': 0.16; 'discarded': 0.16; 'downside': 0.16; 'key):': 0.16; 'nest': 0.16; 'self[key]': 0.16; 'wrote:': 0.18; 'cheap': 0.19; 'mon,': 0.24; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; '(like': 0.30; 'needed.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'easier': 0.31; "d'aprano": 0.31; 'keyerror:': 0.31; 'sep': 0.31; 'steven': 0.31; 'class': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'created': 0.35; 'except': 0.35; 'problem.': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:data': 0.36; 'shows': 0.36; 'should': 0.36; 'easily': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'structure': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'matter': 0.61; 'worth': 0.66; 'potentially': 0.81; 'fortunately': 0.84; 'hand,': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Pp3r+6Sv69e8RlwmaK5/AMyNVuAzaE4t5MzyZr7cZiA=; b=aDWOMW7Ve0F5yhR8FduaTNZZI/uVff/5kP/XpD3fkSshtHAg9VyfkBjj7cYEGwPi1b 58fL1EGzEfUrwHitTlj5Qfo+b3kBjpw4Q2IEhJtsI6jrix4Aj96qmBW1HtmYqJRSJ2gS droz1OY+78KQEwFJdCAPEYssl3rAEEDCcF9djj/3tHjPbY5m4FbRPuHP8SUTbvH9XpPN +h/lzKsOWTBQJBxWVlAv7IxjJbbD46CDmxzre6URs7/Q7rWDcfE+K6ooS7d1XbcLUYvl DcMOaxefKwcJ6vmdM3BrWAJ3+NjZ8P597pia/2sYKZFko2KnJgZ2IAtmKfsS7DhQ+RrK vrHg==
X-Received by 10.70.128.199 with SMTP id nq7mr8193369pdb.156.1412005025400; Mon, 29 Sep 2014 08:37:05 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <54296401$0$12999$c3e8da3$5496439d@news.astraweb.com>
References <mailman.14456.1411949456.18130.python-list@python.org> <54296401$0$12999$c3e8da3$5496439d@news.astraweb.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 29 Sep 2014 09:36:25 -0600
Subject Re: trouble building data structure
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.14474.1412005035.18130.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1412005035 news.xs4all.nl 2898 [2001:888:2000:d::a6]:42638
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78470

Show key headers only | View raw


On Mon, Sep 29, 2014 at 7:52 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Whether you prefer to use setdefault, or a defaultdict, is a matter of
> taste.

There is potentially a significant difference in performance -- with
setdefault, the subordinate data structure is created on every call to
be passed into setdefault, only to be discarded if the key already
exists. With defaultdict, the subordinate data structure is only
created when needed. Dicts are pretty cheap to construct though, and
this is probably not worth fretting over until profiling shows it to
be a problem.

On the other hand, it's easier to nest setdefaults arbitrarily deep
than it is for defaultdicts. This is because defaultdict suffers from
a design flaw -- defaultdict should be a function that returns a class
(like namedtuple), not a class itself. Fortunately that's easily fixable:

_DEFAULT_DICT_CACHE = {}

def defaultdict(callable):
  try:
    return _DEFAULT_DICT_CACHE[callable]
  except KeyError:
    class _defaultdict(dict):
      def __missing__(self, key):
        self[key] = value = self._callable()
        return value

    _DEFAULT_DICT_CACHE[callable] = _defaultdict
    return _defaultdict

A downside is that it would take some extra work to make this picklable.

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


Thread

trouble building data structure David Alban <extasia@extasia.org> - 2014-09-28 17:04 -0700
  Re: trouble building data structure Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-29 23:52 +1000
    Re: trouble building data structure Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-29 09:36 -0600

csiph-web