Is there a canonical way to cache instance methods in python?

I have some computationally intensive functions in my python script that I would like to cache. I went looking for solutions on stack overflow and found lots of links:

  1. https://stackoverflow.com/questions/4431703/python-resettable-instance-method-memoization-decorator
  2. https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
  3. http://pythonhosted.org/cachetools/
  4. https://pythonhosted.org/Flask-Cache/ (I’ve used this one for flask applications, but this one is not a flask application).

In the end, I ended up pasting this into my program. It seems simple enough — and works fine.

class memoized(object):
    '''Decorator. Caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned
    (not reevaluated).
    '''
    def __init__(self, func):
        self.func = func
        self.cache = {}

    def __call__(self, *args):
        if not isinstance(args, collections.Hashable):
            return self.func(*args)
        if args in self.cache:
            return self.cache[args]
        else:
            value = self.func(*args)
            self.cache[args] = value
            return value

    def __repr__(self):
        '''Return the function's docstring.'''
        return self.func.__doc__

    def __get__(self, obj, objtype):
        '''Support instance methods.'''
        return functools.partial(self.__call__, obj)

However, I am wondering if there is a cannonical best practice in Python. I guess I assumed that there would be a very commonly used package to handle this and am confused about why this does not exist. http://pythonhosted.org/cachetools/ is only on version .6 and the syntax is more complex than simply adding a @memoize decorator, like in other solutions.

1

There is no canonical, uniquely Pythonic way to do this. None of which I am aware, at any rate–and I’m speaking as someone who has looked, and who is the author of a successful memoizing package.

However, I believe your lack of found prior art may be a terminology issue as much as anything. You asked for caching. That is a proper term, but it’s overly broad. Caching the results of a particular function call or activity for later use is more specifically referred to as memoizing or memoization. And indeed there are many memoizing packages available from the community, as well as many recipes (for example, this one). I have also seen memoizng functions in many multi-purpose utility packages. Many of them are mature, battle-hardened, and constantly used in production–not mere “version 0.6 code.”

Why memoizing is not more canonically or idiomatically handled I cannot say. Perhaps because there are various ways to accomplish it with differing virtues and tradeoffs. Or perhaps because there are already so many different approaches in use. I often find features–“flatten a list of list” is another–that other language communities eagerly converge around but that the Python community or powers that be seem to prefer handling as recipes rather than committing to a specific API. In any case, if your code works, welcome to the ranks of successful memoizers!

Update

Since the Python 3 standard library (for 3.2 and later) includes an lru_cache decorator (documentation here), I’d have to say that looks like a late-breaking attempt to standardize the most common memoization use case. That it came so late in Python’s evolution is probably why there’s no common solution, but for new code, that’s as close to canonical as you’re going to find.

1

Because an instance method is allowed to use self attributes, and in particular modify them, you can’t guarantee correctness of an arbitrary memoized instance method. Additionally, your implementation adds an implicit constraint on your object to be hashable, and cache hit depending of that hash, you are limited on classes you can use it on and fields you can add to classes having a memoized method (or inheriting one).

For these reasons, if you need to memoize a function it is better design to turn the expensive instance method into a static method, explicitly passing to it necessary object attributes as arguments. This frees your hands on class design, and can improve cache hit. This also helps simplifying and generalizing the memoizing code. Fine-grain implementations exists for this desing, sometimes letting you customize cache size, duration / invalidation methods, guaranteeing thread safety, persistance…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật