Why does my LRU cache skip the same argument?

I have a code that looks like this:

from functools import lru_cache


@lru_cache()
def get_cheese(type):
    print('{}? We\'re all out.'.format(type))
    return None

get_cheese(type='cheddar')
get_cheese('cheddar')
print(get_cheese.cache_info())

cache_info() reports that there were two misses - but I called the function with the same argument.

It actually took some time, but I realized that it was because in one instance I used the arg keyword and the other I used a positional argument.

But why?

+4
source share
1 answer

The shell created functools.lru_cachedoes not attempt to verify or copy the signature of the wrapped function. Python version defined as

def wrapper(*args, **kwargs):
    ...
    key = make_key(args, kwds, typed)
    ...

, , , args kwargs, , - positional keyword. C > , .

, ? . , , , , , . , , .

+2