defaultdict's bug or feature?

Red Forks redforks at gmail.com
Fri May 22 20:30:50 EDT 2009


On Sat, May 23, 2009 at 2:03 AM, Rhodri James
<rhodri at wildebst.demon.co.uk>wrote:

> I asked you not to top-post.  Please put your replies *below* the
> messages you're quoting, not above.  It's much easier to understand
> the conversation that we're having if you do that, and much more
> aggravating if you don't.
>
I misunderstand you last email, thanks.

>
> On Fri, 22 May 2009 09:53:04 +0100, Red Forks <redforks at gmail.com> wrote:
>
>  Yes, you maybe right. When use defaultdict, should not rely get() method
>> anymore, d[] is just enough.
>>
>
> Almost.  You should rely on get() to do what it says, not what you think
> it should do.  That's generally true, by the way; when the Fine Manual
> says that a class, function or method will do one thing, expecting it to
> do something else is unreasonable.
>
>  Actually, I steal defaultdict to do other things:
>>
>> class OnloadDict(defaultdict):
>>    def __init__(self, onload):
>>        self.__onload = onload
>>
>>    def __missing__(self, key):
>>        result = self.__onload(key)
>>        if not result:
>>            raise KeyError
>>
>>        self[key] = result
>>        return result
>>
>>    def get(self, key, default = None):
>>        ''' defaultdict.get() won't call __missing__, so override '''
>>        try:
>>            return self[key]
>>        except KeyError:
>>            return default
>>
>> OnloadDict, is like a cache. When the value is not in dict, using 'onload'
>> function to load the value by the key.
>> When no value correspond to a key, a KeyError will raised. So I need a
>> *safer* version 'get()' method.
>>
>
> Why are you so determined to (ab)use get() on your class?  You should
> only be calling OnloadDict.get() if you really mean "get me the value
> associated with this key, or the default I'm telling you about *right*
> *now* if there isn't one."

The key point is: "what key/value pairs should in defaultdict". I think any
key/default pair(they'll add to dict when you ask for it), maybe you think
only manual added pairs.
defaultdict break dict rules, that a query method (d[key]) should not modify
dict, so another query method (the get() method) break default rule is not a
big deal.
Never mind, my "get()" method hack works OK.

>
>
> Aside from that, this is neat.  Strictly you should call
> defaultdict.__init__(self) in your __init__() function just in case
> defaultdict needs any setup that you aren't doing (which it does, but
> you override __missing__() so it never notices that it doesn't have
> a default_factory).  You get away with it here, but it's not a good
> habit to get into.
>
I'm new to python, adding  'defaultdict.__init__()' call would be nice. In
other languages, base class's default constructor is automaticlly called.

>
> --
> Rhodri James *-* Wildebeeste Herder to the Masses
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090523/3a9bf470/attachment.html>


More information about the Python-list mailing list