dictionary that discards old items

Steven Bethard steven.bethard at gmail.com
Sun Jul 24 04:07:25 CEST 2005


[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. ;)

STeVe



More information about the Python-list mailing list