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


Groups > comp.lang.python > #78470

Re: trouble building data structure

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 2014-09-29 09:36 -0600
Subject Re: trouble building data structure
Newsgroups comp.lang.python
Message-ID <mailman.14474.1412005035.18130.python-list@python.org> (permalink)

Show all headers | 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