Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102926
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Herman <sorsorday@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | How to properly override the default factory of defaultdict? |
| Date | Sun, 14 Feb 2016 16:17:44 -0800 |
| Lines | 34 |
| Message-ID | <mailman.122.1455495511.22075.python-list@python.org> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de yVPs9G9wLYUq+JaKhN2h6wTUswLrNX9Xs0M3KuAEP5sg== |
| Return-Path | <sorsorday@gmail.com> |
| 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; '"""': 0.05; 'constructor': 0.07; 'key.': 0.07; 'reason,': 0.07; 'subject:How': 0.09; 'key)': 0.09; 'normally,': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'def': 0.13; 'argument': 0.15; '"lambda"': 0.16; 'args:': 0.16; 'argument.': 0.16; 'defaultdict': 0.16; 'given)': 0.16; 'key):': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:default': 0.16; 'try:': 0.18; 'arguments': 0.22; 'keyerror:': 0.22; 'pass': 0.22; 'passing': 0.23; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'ex:': 0.29; 'code:': 0.29; 'skip:_ 10': 0.32; 'skip:d 40': 0.32; 'getting': 0.33; 'class': 0.33; 'except': 0.34; 'skip:d 20': 0.34; 'skip:& 20': 0.35; 'received:google.com': 0.35; '8bit%:4': 0.35; 'but': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'skip:& 10': 0.37; 'expect': 0.37; 'missing': 0.37; 'received:209': 0.38; 'received:209.85.220': 0.38; 'subject:the': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'some': 0.40; '**kw)': 0.84; '**kw):': 0.84; "class's": 0.84 |
| 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=GC+vm9zrK3aGMh52BoBw8fbF53hcYC5r73MmDFkNajo=; b=B4qjGBUbtOmf+fkUaFFCNHYmgJK4BScEDI4wZzntFWfSw17+Bw6dLMl3DCGWuy7gPW 6EoD7JUqyIDAJ76ESBSxv2SXLxkId0B951iMxWpB6YcnkR4Oy1yLkELb/boDoeHDzIoG 8djeAI7kBJY2HiJhDWR+p5+vMHQSOIR+pCnWbg4doOVQL3layNSz82yfJV/qAKiAREzk 46h1v6VAOmV1A//60qHpvEFqqzJ5hvCWPrJ7NQ5RcjaB9mOwfovUImBzuruj/4bgnLy7 uffbL6uWqpd6Pe3ZpguXKiW92xrFo+eECAH/h5YHesdR37awV0HerI/DcopVDnSAVVaR g1BA== |
| 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=GC+vm9zrK3aGMh52BoBw8fbF53hcYC5r73MmDFkNajo=; b=dEHp13sYyIPLb5k1iLxHO8wrtiOeDKqy2KhG0d1QQuNJjarxMS10yI5Z23HKYFGCA8 lzE+apRVOAA9mhOkxv9vQAF4Zb743H6e/BzMTasyLV7BRUH39qYto4lBmcPkN1ZdbpZS +5CB1en+dmpUek4DK7/YH3+RheiMCdu/Ot8K+QHX5Vs8jI73I5OaAZRXvG3ZuxaCf846 Wa20g6N+ENPu5UzqKH/P09v+tirxyl+NdtuHJ7B+J0y97YKehzGUV9BjZbNCqUL0NivW rnH5K+haKzPIDqC/1g4NNsyBSOlhTc5x+7wyc3qhQWU3l3uzKi55iUpJD1+Ujt5RxSAg uBUQ== |
| X-Gm-Message-State | AG10YOQg37CNALDuqY5w6YzOWZvV1fKiZ0+ioiYiODY2RH/t9hz2BLaM4Vm4Z90AxdLdrjnNYTV2Y5av2TmoYg== |
| X-Received | by 10.55.82.214 with SMTP id g205mr17119593qkb.72.1455495503552; Sun, 14 Feb 2016 16:18:23 -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:102926 |
Show key headers only | View raw
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:
class DefaultDictWithEnhancedFactory(defaultdict):
"""Just like the standard python collections.dict,
but the default_factory takes the missing key as argument.
Args:
default_factory: A function that takes the missing key as the
argument
and return a value for the missing key.
*a: arguments passing to the defaultdict constructor
**kw: keyword arguments passing to the defaultdict constructor
"""
def __init__(self, default_factory, *a, **kw):
defaultdict.__init__(self, default_factory, *a, **kw)
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
# Normally, you would expect this line to be
# called for missing keys...
return self.default_factory(key)
except TypeError as ex:
# However, this is actually getting called
# because for some reason, defaultdict still
# intercepts the __getitem__ call and raises:
# TypeError: <lambda>() takes exactly 1 argument (0 given)
# So we have to catch that instead...
if "lambda" in str(ex):
return self.default_factory(key)
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
How to properly override the default factory of defaultdict? Herman <sorsorday@gmail.com> - 2016-02-14 16:17 -0800 Re: How to properly override the default factory of defaultdict? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-15 14:56 +1100 Re: How to properly override the default factory of defaultdict? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-02-15 21:28 +1300
csiph-web