Yes, you maybe right. When use defaultdict, should not rely get() method anymore, d[] is just enough. When a function return a defaultdict, but people don't know it, so:<br><br>d = load_map()<br># if she call d['a'], everything is OK but<br>
# when call d.get('a'), she is always get None.<br># Why she call d.get('a'), not d['a'], because she don't want to provider default value herself (she didn't know it is a defaultdict)<br>It is just wield, call d[''a'] Ok, not get('a').<br>
<br>Sorry my poor english. Maybe somebody don't catch my mind, or maybe a little rude. <br>I'm using and learning python about 2 weeks ago, spent me three hrs to find the problem, almost drive me mad. <br>Maybe other like me, find some inconvience on this.<br>
<br>--------------------------------------------------------------------------------------<br><br>Actually, I steal defaultdict to do other things:<br><br>class OnloadDict(defaultdict):<br>    def __init__(self, onload):<br>
        self.__onload = onload<br><br>    def __missing__(self, key):<br>        result = self.__onload(key)<br>        if not result:<br>            raise KeyError<br><br>        self[key] = result<br>        return result<br>
<br>    def get(self, key, default = None):<br>        ''' defaultdict.get() won't call __missing__, so override '''<br>        try:<br>            return self[key]<br>        except KeyError:<br>
            return default<br><br>OnloadDict, is like a cache. When the value is not in dict, using 'onload' function to load the value by the key.<br>When no value correspond to a key, a KeyError will raised. So I need a *safer* version 'get()' method.<br>
<br><div class="gmail_quote">On Fri, May 22, 2009 at 12:35 PM, Rhodri James <span dir="ltr"><<a href="mailto:rhodri@wildebst.demon.co.uk">rhodri@wildebst.demon.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Please don't top-post, it makes the thread of argument hard to follow.<div class="im"><br>
<br>
On Fri, 22 May 2009 01:44:37 +0100, Red Forks <<a href="mailto:redforks@gmail.com" target="_blank">redforks@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
You mean 'get' method should not alter the dict, does 'dict[key]' should not<br>
alter the dict either?<br>
<br>
d = defaultdict(set)<br>
assert len(d) == 0<br>
print d[1]<br>
assert len(d) == 1<br>
<br>
auto insert value to dict, when value is not in dict, is what defaultdict<br>
try to do.<br>
</blockquote>
<br></div>
Behaviour you are deliberately avoiding by calling `get`, since that<br>
explicitly behaves the same way that it does for dicts.  You're doing<br>
two different things through two different interfaces with two different<br>
specs.  Why are you expecting them to have the same effect?<div><div></div><div class="h5"><br>
<br>
-- <br>
Rhodri James *-* Wildebeeste Herder to the Masses<br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>