Create a gist now

Instantly share code, notes, and snippets.

class HashableFunc(object):
This class acts as a decorator to make a function persistently hashable
relevant_attr_names = ['co_argcount', 'co_cellvars', 'co_code', 'co_consts',
'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars',
'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize',
def relevent_attrs(self):
return tuple(getattr(func.__code__,s) for s in relevant_attr_names)
def __init__(self, func):
self.func = func
def __hash__(self):
return hash(self.relevant_attrs())
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
def __eq__(self, other):
return self.relevant_attrs() == other.relevant_attrs
def fib(a):
if a <= 1:
return 1
return fib(a-1) + fib(a-2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment