Re: dictionary that discards old items
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: dictionary that discards old items

From: Bengt Richter <bokr@oz.net>
Date: Sun Jul 24 2005 - 08:22:13 CEST

On Sat, 23 Jul 2005 20:07:25 -0600, Steven Bethard <steven.bethard@gmail.com> wrote:

>[Raymond Hettinger]
>>>class Cache(dict):
>>> def __init__(self, n, *args, **kwds):
>>> self.n = n
>>> self.queue = collections.deque()
>>> dict.__init__(self, *args, **kwds)
>
>[Bengt Richter]
>> Minor comment: There is a potential name collision problem for keyword n=something,
>> so what is considered best practice to avoid that? __n or such as the n arg?
>
>I don't know what best practice is, but if you want to guarantee to
>avoid the name collision, you can write:
>
>def __init__(*args, **kwargs):
> self = args[0]
> self.n = args[1]
> self.queue = collections.deque()
> dict.__init__(self, *args[2:], **kwargs)
>
>It's not pretty though. ;)
Bullet proofing doesn't have to be ;-)
Best solution I've seen yet, thanks.

Regards,
Bengt Richter
Received on Thu Sep 29 17:08:05 2005