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


Groups > comp.lang.python > #80150

Dictionaries with variable default.

Date 2014-11-03 12:04 +0100
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Subject Dictionaries with variable default.
Newsgroups comp.lang.python
Message-ID <mailman.15424.1415012757.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)

-- 
Antoon Pardon

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


Thread

Dictionaries with variable default. Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-11-03 12:04 +0100

csiph-web