Message152083
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. |
|
Date |
User |
Action |
Args |
2012-01-27 10:50:03 | joern | set | recipients:
+ joern |
2012-01-27 10:50:03 | joern | set | messageid: <1327661403.58.0.135329216168.issue13887@psf.upfronthosting.co.za> |
2012-01-27 10:50:02 | joern | link | issue13887 messages |
2012-01-27 10:50:02 | joern | create | |
|