我至少有一個非常醜陋的解決方案。將db_handle
包裝在始終等於的對象中,並將其解包到函數中。
它需要一個有很多輔助函數的裝飾器,這使得堆棧跟蹤相當混亂。
class _Equals(object):
def __init__(self, o):
self.obj = o
def __eq__(self, other):
return True
def __hash__(self):
return 0
def lru_cache_ignoring_first_argument(*args, **kwargs):
lru_decorator = functools.lru_cache(*args, **kwargs)
def decorator(f):
@lru_decorator
def helper(arg1, *args, **kwargs):
arg1 = arg1.obj
return f(arg1, *args, **kwargs)
@functools.wraps(f)
def function(arg1, *args, **kwargs):
arg1 = _Equals(arg1)
return helper(arg1, *args, **kwargs)
return function
return decorator
來源
2015-06-09 16:53:48
WGH