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


Groups > comp.lang.python > #102928

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

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: How to properly override the default factory of defaultdict?
Date Mon, 15 Feb 2016 11:36:45 +1100
Lines 53
Message-ID <mailman.123.1455496624.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
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de BLf/hUZ+kdTyaHWa7TSvpgwU69p3g/BM1t/f8TykLsvg==
Cancel-Lock sha1:SCL5jefjFGq3AzaTd82XgnRxxAk=
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'reason,': 0.07; 'subject:How': 0.09; 'key)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'def': 0.13; '(about': 0.16; 'args:': 0.16; 'argument.': 0.16; 'arguments;': 0.16; 'byron': 0.16; 'code),': 0.16; 'defaultdict': 0.16; 'herman': 0.16; 'hierarchy': 0.16; 'inheritance': 0.16; 'key):': 0.16; "method's": 0.16; 'nevertheless': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:default': 0.16; 'string': 0.17; 'try:': 0.18; '(see': 0.20; 'do.': 0.22; 'not,': 0.22; 'are.': 0.22; 'doc': 0.22; 'pass': 0.22; 'code,': 0.23; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'pep': 0.29; 'code:': 0.29; 'skip:_ 10': 0.32; 'skip:d 40': 0.32; 'class': 0.33; "i'll": 0.33; 'skip:d 20': 0.34; 'something': 0.35; 'comment': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'instead': 0.36; '(and': 0.36; 'others.': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'missing': 0.37; 'things': 0.38; 'itself': 0.38; 'brief': 0.38; 'mean': 0.38; 'subject:the': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'more': 0.63; 'numerous': 0.66; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; '**kw)': 0.84; '**kw):': 0.84; '_o__)': 0.84; "class's": 0.84; 'complex.': 0.84; 'received:125': 0.84; 'dare': 0.91; 'migrating': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
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:102928

Show key headers only | View raw


Herman <sorsorday@gmail.com> writes:

> 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:

I don't see an example of using that code, so that we can use it the
same way you are.

So I'll comment on some things that may not be relevant but nevertheless
should be addressed:

> class DefaultDictWithEnhancedFactory(defaultdict):
>     """Just like the standard python collections.dict,
>     but the default_factory takes the missing key as argument.

Your doc string should have only a brief (about 50–60 characters) single
line synopsis. If it's longer, you may be writing something too complex.
(See PEP 257.)

>     Args:

The class itself doesn't take arguments; its numerous methods do. I
think you mean these descriptions to be in the ‘__init__’ method's doc
string.

>     def __init__(self, default_factory, *a, **kw):
>         defaultdict.__init__(self, default_factory, *a, **kw)
>
>     def __getitem__(self, key):
>         try:
>             return dict.__getitem__(self, key)

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)

If you're not using Python 3 (and you should, for new code), ‘super’ is
a little more complex. Migrating to Python 3 has this advantage among
many others.

-- 
 \       "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

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


Thread

Re: How to properly override the default factory of defaultdict? Ben Finney <ben+python@benfinney.id.au> - 2016-02-15 11:36 +1100

csiph-web