odd behavoiur seen

Chris Hinsley chris.hinsley at gmail.com
Mon Jul 22 20:36:41 CEST 2013


Folks, I have this decorator:

def memoize(maxsize):
    def _memoize(func):
        lru_cache = {}
        lru_list = []

        def memoizer(*args, **kwargs):
            key = str(args) + str(kwargs)
            if key in lru_cache:
                lru_list.remove(key)
                lru_list.append(key)
                return lru_cache[key]
            print len(lru_list),
            if len(lru_list) >= maxsize:
                del(lru_cache[lru_list[0]])
                del(lru_list[0])
            ret = func(*args, **kwargs)
            lru_cache[key] = ret
            lru_list.append(key)
            return ret
        return memoizer
    return _memoize

I didn't used to do the 'len(lru_list) >= maxsize' just '==' and 
noticed it sailing past the max number of entries, so put in the print 
statement, and now I see it ocationally printing a value 1 larger than 
maxsize !!!

So if I use it as '@memoize(64)' I see some 65's in the output ! I'm at 
a loss to explain it, does anyone knows why ? Is it a bug or some 
threading issue ? I'm not useing threads BTW, and I've noticed this in 
both running it with Python or Pypy.

Best Regards

Chris




More information about the Python-list mailing list