如果你要为它编写一个完整的函数,为什么不复制粘贴原始源代码并在那里实现你的逻辑?
def lru_cache(maxsize=128, typed=False, cache=False):
if isinstance(maxsize, int):
if maxsize < 0:
maxsize = 0
elif callable(maxsize) and isinstance(typed, bool):
user_function, maxsize = maxsize, 128
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
return update_wrapper(wrapper, user_function)
elif maxsize is not None:
raise TypeError('Expected first argument to be an integer, a callable, or None')
def decorating_function(user_function):
if not cache:
return user_function # We add here
wrapper = _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo)
return update_wrapper(wrapper, user_function)
return decorating_function
通过这种方式,您可以更好地控制函数,并且可以轻松查看所有内容,并且可以通过这种方式玩转 functools 的内部。