Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2d4b3d

Browse files
committed
Issue #28991: Address comment that the __len__ call looked unattractive
1 parent 92c481a commit b2d4b3d

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

Lib/functools.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
493493
hits = misses = 0
494494
full = False
495495
cache_get = cache.get # bound method to lookup a key or return None
496+
cache_len = cache.__len__ # get cache size without calling len()
496497
lock = RLock() # because linkedlist updates aren't threadsafe
497498
root = [] # root of the circular doubly linked list
498499
root[:] = [root, root, None, None] # initialize by pointing to self
@@ -574,16 +575,16 @@ def wrapper(*args, **kwds):
574575
last = root[PREV]
575576
link = [last, root, key, result]
576577
last[NEXT] = root[PREV] = cache[key] = link
577-
# Use the __len__() method instead of the len() function
578+
# Use the cache_len bound method instead of the len() function
578579
# which could potentially be wrapped in an lru_cache itself.
579-
full = (cache.__len__() >= maxsize)
580+
full = (cache_len() >= maxsize)
580581
misses += 1
581582
return result
582583

583584
def cache_info():
584585
"""Report cache statistics"""
585586
with lock:
586-
return _CacheInfo(hits, misses, maxsize, cache.__len__())
587+
return _CacheInfo(hits, misses, maxsize, cache_len())
587588

588589
def cache_clear():
589590
"""Clear the cache and cache statistics"""

0 commit comments

Comments
 (0)