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


Groups > comp.lang.python > #80151

Re: Dictionaries with variable default.

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'argument': 0.05; 'cc:addr :python-list': 0.11; 'def': 0.12; 'dependant': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'given)': 0.16; 'key):': 0.16; 'key?': 0.16; 'subject:variable': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'header :In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; '>>>>': 0.31; 'work:': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'implement': 0.38; 'nov': 0.38; 'pm,': 0.38; 'recent': 0.39; 'default': 0.69; 'hoping': 0.75; 'pardon': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=axd+XQ9BomQk2TLpWLTmr/6726ffFkt8oJXOB07uwms=; b=GwpdZr6pAcdTi3qecy+YwVglILjpDoJVB2AXfLgGFwYd7tjfaIAzcLiOfpzvPqPRrv z51kRxj64nSrEHi6g79pZUdKMQgONqlG0wO46HoVYmDymCuZUTpUfIrNV3swGwEKlTA8 9VuPVfkCvO/ml+Bjx8yPKnqFuiCcKyO5zmbRn6QmTdhFhMdhQ7W74PbzQuI5exOJUfLg 23V2kJPoFTg3MFdRTY4xVXzz8zV+iIMYs6nSL8NMRUomN8O2Zd87vfaSbJGQIxPkCIar Q9iifZfxi+BHMB8VDPWklZ5AjCxf2H+AKb2frQRWhTIag8iIvYIpvSJt7V7Bs1ajQ6B1 mroQ==
MIME-Version 1.0
X-Received by 10.107.130.218 with SMTP id m87mr46088775ioi.8.1415012997431; Mon, 03 Nov 2014 03:09:57 -0800 (PST)
In-Reply-To <54576150.6090002@rece.vub.ac.be>
References <54576150.6090002@rece.vub.ac.be>
Date Mon, 3 Nov 2014 22:09:57 +1100
Subject Re: Dictionaries with variable default.
From Chris Angelico <rosuav@gmail.com>
Cc Pyton List <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.15425.1415013405.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415013405 news.xs4all.nl 2878 [2001:888:2000:d::a6]:58343
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 3599
X-Received-Body-CRC 322481097
Xref csiph.com comp.lang.python:80151

Show key headers only | View raw


On Mon, Nov 3, 2014 at 10:04 PM, Antoon Pardon
<antoon.pardon@rece.vub.ac.be> wrote:
> Is it possible to have a default dictionary where the default is dependant
> on the key?
>
> I was hoping something like this might work:
>>>> m = defaultdict(lambda key: key+1)
>
> But it obviously doesn't:
>>>> m[3]
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: <lambda>() takes exactly 1 argument (0 given)

Yes, but not with the defaultdict. Instead, just implement __missing__:

class KeyPlusOne(dict):
    def __missing__(self, key): return key+1

ChrisA

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


Thread

Re: Dictionaries with variable default. Chris Angelico <rosuav@gmail.com> - 2014-11-03 22:09 +1100

csiph-web