记忆和封装 [英] Memoization and encapsulation
问题描述
我正在玩简单的记忆,想出了一些东西
这样:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache [x]
else:
result = x + 1#或耗时的计算...
_cache [x] =结果
返回结果
如果我能以某种方式将缓存绑定到函数,它就会击中我,我可以
摆脱那个讨厌的全局变量。
我试过这个:
I was playing around with simple memoization and came up with something
like this:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache[x]
else:
result = x+1 # or a time consuming calculation...
_cache[x] = result
return result
when it hit me if I could somehow bind the cache to the function, I could
get rid of that pesky global variable.
I tried this:
def func(x):
....尝试:
.... func.cache
....除了AttributeError:
.... func。 cache = {}
.... if func.cache.has_key(x):
.... return func.cache [x]
....其他:
....结果= x + 1
.... fu nc.cache [x] =结果
....返回结果
它按预期工作,但缺乏优雅。
我也可以使用一个新式的类来代替使用函数,就好像它是一个函数:
类Func(对象) :
def func(x): .... try:
.... func.cache
.... except AttributeError:
.... func.cache = {}
.... if func.cache.has_key(x):
.... return func.cache[x]
.... else:
.... result = x + 1
.... func.cache[x] = result
.... return result
and it works as expected, but it lacks elegance.
Instead of using a function, I can also use a new-style class as if it
were a function:
class Func(object):
.... cache = {}
.... def __new __(self,x):
....如果self.cache.has_key(x):
....返回self.cache [x]
.. ..其他:
....结果= x + 1
.... self.cache [x] =结果
。 ...返回结果
又一次有效,但我不能不觉得这是滥用课程
机制来做到这一点。
人们怎么想?还有更好的办法吗?
-
史蒂文。
.... cache = {}
.... def __new__(self, x):
.... if self.cache.has_key(x):
.... return self.cache[x]
.... else:
.... result = x+1
.... self.cache[x] = result
.... return result
and again it works, but I can''t help feeling it is an abuse of the class
mechanism to do this.
What do folks think? Is there a better way?
--
Steven.
推荐答案
Steven D''Aprano写道:
Steven D''Aprano wrote:
我正在玩简单的记忆,想出了像这样的东西:
_cache = {}
def func(x):
全局_cache
如果_cache.has_key(x):
返回_cache [x]
否则:
结果= x + 1#或耗时的计算...
_cache [x] =结果
返回结果
当它击中我时如果我可以某种方式将缓存绑定到函数,我可以摆脱那个讨厌的全局变量。
I was playing around with simple memoization and came up with something
like this:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache[x]
else:
result = x+1 # or a time consuming calculation...
_cache[x] = result
return result
when it hit me if I could somehow bind the cache to the function, I could
get rid of that pesky global variable.
除了暴露
缓存?
def func(x,_cache = {}):
what would be the problem of the following, other than exposing the
cache which the caller may override ?
def func(x,_cache={}):
Steven D''Aprano写道:
Steven D''Aprano wrote:
我正在玩弄简单的记忆,想出了像这样的东西:
_cache = {}
def func(x):
global _cache
if _cache.has_key (x):
返回_cache [x]
否则:
结果= x + 1#或耗时的计算......
_cache [x] =结果>返回结果
如果我能以某种方式将缓存绑定到函数,它就会打击我,我可以摆脱那个讨厌的全局变量。
I was playing around with simple memoization and came up with something
like this:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache[x]
else:
result = x+1 # or a time consuming calculation...
_cache[x] = result
return result
when it hit me if I could somehow bind the cache to the function, I could
get rid of that pesky global variable.
尝试这样的事情:
def func(x,_cache = {}):
如果x在缓存中:
返回缓存[x]
结果= x + 1#或耗时的功能
返回结果
*如果缓存命中是常态,那么try / except版本会更快一点
。
*在原始代码中,全局声明不需要因为
对_cache的分配没有改变,而是它的内容是。
Raymond
Try something like this:
def func(x, _cache={}):
if x in cache:
return cache[x]
result = x + 1 # or a time consuming function
return result
* If cache hits are the norm, then a try/except version would be a bit
faster.
* In your original code, the global declaration wasn''t needed because
the assigment to _cache wasn''t changed, rather its contents were.
Raymond
Steven D''Aprano写道:
Steven D''Aprano wrote:
我正在玩简单的memoization并想出了一些东西
像这样:
_cache = {}
def func(x):
全局_cache
如果_cache.has_key(x):
返回_cache [x]
否则:
结果= x + 1#或耗时的计算...
_cache [x] =结果
返回结果
>如果我能以某种方式将缓存绑定到函数中,它就会打击我,我可以摆脱那个讨厌的全局变量。
I was playing around with simple memoization and came up with something
like this:
_cache = {}
def func(x):
global _cache
if _cache.has_key(x):
return _cache[x]
else:
result = x+1 # or a time consuming calculation...
_cache[x] = result
return result
when it hit me if I could somehow bind the cache to the function, I could
get rid of that pesky global variable.
尝试这样的事情:
def func(x,_cache = {}):
如果_cache中的x:
返回_cache [x]
结果= x + 1#或耗时的功能
_cache [x] =结果
返回结果
>
*如果缓存命中是常态,那么try / except版本会更快一点
。
* in y我们的原始代码,全局声明是不需要的,因为
对_cache的分配没有改变,而是它的内容是。
Raymond
Try something like this:
def func(x, _cache={}):
if x in _cache:
return _cache[x]
result = x + 1 # or a time consuming function
_cache[x] = result
return result
* If cache hits are the norm, then a try/except version would be a bit
faster.
* In your original code, the global declaration wasn''t needed because
the assigment to _cache wasn''t changed, rather its contents were.
Raymond
这篇关于记忆和封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!