New submission from Jörn Hees <nrej9...@joernhees.de>: 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. ---------- components: Extension Modules, Library (Lib) messages: 152083 nosy: joern priority: normal severity: normal status: open title: defaultdict.get does not default to initial default but None type: behavior versions: Python 2.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue13887> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com