*如果缓存命中是常态,那么try / except版本会更快一点 。 * in y我们的原始代码,全局声明是不需要的,因为 对_cache的分配没有改变,而是它的内容是。 RaymondTry something like this:def func(x, _cache={}):if x in _cache:return _cache[x]result = x + 1 # or a time consuming function_cache[x] = resultreturn result* If cache hits are the norm, then a try/except version would be a bitfaster.* In your original code, the global declaration wasn''t needed becausethe assigment to _cache wasn''t changed, rather its contents were.Raymond 这篇关于记忆和封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 19:41