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: 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 Cc: Pyton List 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Mon, Nov 3, 2014 at 10:04 PM, Antoon Pardon 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 "", line 1, in > TypeError: () 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