This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author joern
Recipients joern
Date 2012-01-27.10:50:02
SpamBayes Score 7.9729e-12
Marked as misclassified No
Message-id <1327661403.58.0.135329216168.issue13887@psf.upfronthosting.co.za>
In-reply-to
Content
I wanted to create a "function registrar" d using a defaultdict. The library that this registrar is passed to expects it to return functions taking 3 args. Now if the first call is d.get(x) it seems that in contrast to d[x] the default arg of get is returned (None) instead of the defaultdicts default. If i call d[x] first and then d.get(x) i get what i expected.

Example:

In [1]: def foo(a,b,c):
   ...:     return (a,b,c)
   ...: 

In [2]: from collections import defaultdict

In [3]: d = defaultdict(lambda:foo)

In [4]: d.get(1)

In [5]: d[1]
Out[5]: <function foo at 0x1015a2ed8>

In [6]: d.get(1)
Out[6]: <function foo at 0x1015a2ed8>

In [7]: d.get(2)(1,2,3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/joern/<ipython console> in <module>()

TypeError: 'NoneType' object is not callable

In [8]: d[2](1,2,3)
Out[8]: (1, 2, 3)

In [9]: d.get(2)(1,2,3)
Out[9]: (1, 2, 3)


I'm not sure this is the desired behavior, but it wasn't quite what i expected from a dictionary with a default.
If it is the desired behavior the documentation of defaultdict should include an explanation what happens.
History
Date User Action Args
2012-01-27 10:50:03joernsetrecipients: + joern
2012-01-27 10:50:03joernsetmessageid: <1327661403.58.0.135329216168.issue13887@psf.upfronthosting.co.za>
2012-01-27 10:50:02joernlinkissue13887 messages
2012-01-27 10:50:02joerncreate