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


Groups > comp.lang.python > #103227

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date Fri, 19 Feb 2016 17:24:40 -0700
Lines 44
Message-ID <mailman.6.1455927922.13884.python-list@python.org> (permalink)
References <CANOe_mhpR2AMYruiwfawOvoiupJjOiswK7eZ14557KEvcc4Khg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de ps7Y4XhAUsKApli08o6U3Ai7qoJXCsY71NAvuhXG1vUg==
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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'true,': 0.04; 'error:': 0.05; 'constructor': 0.07; 'override': 0.07; 'reason,': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; '__init__': 0.09; 'key)': 0.09; 'skipped.': 0.09; 'substitution': 0.09; 'typeerror:': 0.09; 'violates': 0.09; 'python': 0.10; 'thu,': 0.15; '2016': 0.16; 'byron': 0.16; 'cc:name:python': 0.16; 'defaultdict': 0.16; 'herman': 0.16; 'hierarchy': 0.16; 'inheritance': 0.16; 'iterable': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:default': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'thanks.': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'suggested': 0.20; 'not,': 0.22; 'pass': 0.22; 'am,': 0.23; 'feb': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'arguments,': 0.29; 'be:': 0.29; 'code': 0.30; 'class.': 0.30; 'skip:s 30': 0.31; 'class': 0.33; 'instances': 0.33; 'instead,': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'expected': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'does': 0.39; 'subject:the': 0.39; 'from:': 0.39; 'called': 0.40; 'your': 0.60; 'different': 0.63; 'skip:\xe2 10': 0.70; '**kw)': 0.84; '2.7.': 0.84; '_o__)': 0.84; 'dict,': 0.84; 'signature.': 0.84; 'dare': 0.91; 'technically': 0.91
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 :cc:content-transfer-encoding; bh=KqqKX+5LvaN07T/01CzROazG0YnXaUBfw5A80jgbvy4=; b=C/QpHWS7D6vIKX8HZuq89jiBzqNIDp+eEYAKw60ywHjFEfbGijRzJudVGtLWJznIke kcdZ02q+VRWpOyujXFw53KwBAiITEUJKWalARGR71HE1+uLO6Jgj3Nwb2a9YiYBcJh2+ a2HDwDoNjbNzmrNXGsE6aY/XW9KOlbpSCLM2w46ckqQnENuZ8mded/aWT5IL3qPxyPmS v5CD6DQHUo7wHDQFju6Dw2d9IbF302rKvgx0u7CCar5ZzlMtuldhOrFxVPsodYmXu0xs sN2GsebtRbl82q1q3GvIAbD0nEqJKBMnwc3NCqfefxONdc08Zr3d/MInBCzCepwcYcSA fqDQ==
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:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=KqqKX+5LvaN07T/01CzROazG0YnXaUBfw5A80jgbvy4=; b=kO+oKuaEAkkYYkgDkOpb768NRVIqftekHtVvlQonb00pESVTtArHy9R3W14K/cvjef siLtnpffNNB5UpdqoM6teYLT4YfvqBhulfICCLZomkQUTgBz9e4fjsCYcbi0RWfWxkzr rF+Kd4A6wuAw2uYdSlWwFjSCP6s9shdSo+8zScaFQkB1VXJviiEdDva/WvY89MLQfYAV mQv228XaxanT9J/s+uVGPpP49JenrfnLtB3Kk7wJ9RUbOL7GYq/V2IKMfumJb55cQmVs vdCKY/RPGmKUMeHoD6xi4HvpXe/GIBnyc/3qNL0ZHqX1LgS16qzoDqHLXy8DNCd4Gj9h c9Qw==
X-Gm-Message-State AG10YOSZNk7/jaBOMPKLIVqaTCLvWKAdBIywgG0g15fZNmcLu2Ig3V2SuVOKQrommbD5xadrKI8ufzhqv8ZbGg==
X-Received by 10.50.88.74 with SMTP id be10mr11868518igb.93.1455927919582; Fri, 19 Feb 2016 16:25:19 -0800 (PST)
In-Reply-To <CANOe_mhpR2AMYruiwfawOvoiupJjOiswK7eZ14557KEvcc4Khg@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:103227

Show key headers only | View raw


On Thu, Feb 18, 2016 at 10:41 AM, Herman <sorsorday@gmail.com> wrote:
> 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

You're using it incorrectly. If your class is named
DefaultDictWithEnhancedFactory, then the super call would be:

super(DefaultDictWithEnhancedFactory, self).__init__(default_factory, *a, **kw)

You pass in the current class so that super can look up the next
class. If you pass defaultdict instead, super will think that it's
being called *by* defaultdict and call the __init__ method on its own
superclass, dict, which has a different signature.
defaultdict.__init__ is effectively skipped.

> Look like inheriting from defaultdict is easier. I don't even  have to
> override the constructor as suggested by Chris Angelico above. Thanks.

True, although there's a faint code smell as this technically violates
the Liskov Substitution Principle; the default_factory attribute on
defaultdict instances is expected to be a function of zero arguments,
not one.

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


Thread

Re: How to properly override the default factory of defaultdict? Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-19 17:24 -0700

csiph-web