Re: Dictionaries with variable default.

2014-11-04 Thread Cousin Stanley
> So How should I call this: > > class ...dict(dict): > def __init__(self, fun): > self.fun = fun > > def __missing__(self, key): > return self.fun(key) I don't know how you should, but I tried the following which seems to work class KeyPlusOne( dict ) : def __

Re: Dictionaries with variable default.

2014-11-04 Thread Antoon Pardon
Op 03-11-14 om 12:09 schreef Chris Angelico: > 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

Re: Dictionaries with variable default.

2014-11-03 Thread Chris Angelico
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 rece

Dictionaries with variable default.

2014-11-03 Thread Antoon Pardon
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 ar