dictionary that discards old items

Bengt Richter bokr at oz.net
Sun Jul 24 02:22:13 EDT 2005


On Sat, 23 Jul 2005 20:07:25 -0600, Steven Bethard <steven.bethard at 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



More information about the Python-list mailing list