Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 444731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:18:00+00:00 2026-05-12T21:18:00+00:00

I have a working memoize decorator which uses Django’s cache backend to remember the

  • 0

I have a working memoize decorator which uses Django’s cache backend to remember the result of a function for a certain amount of time. I am specifically applying this to a class method.

My decorator looks like:

def memoize(prefix='mysite', timeout=300, keygenfunc=None):
    # MUST SPECIFY A KEYGENFUNC(args, kwargs) WHICH MUST RETURN A STRING
    def funcwrap(meth):

      def keymaker(*args, **kwargs):
        key = prefix + '___' + meth.func_name + '___' + keygenfunc(args, kwargs)
        return key

      def invalidate(*args, **kwargs):
        key = keymaker(*args, **kwargs)
        cache.set(key, None, 1)

      def newfunc(*args, **kwargs):
        # construct key
        key = keymaker(*args, **kwargs)

        # is in cache?
        rv = cache.get(key)

        if rv is None:
          # cache miss
          rv = meth(*args, **kwargs)
          cache.set(key, rv, timeout)

        return rv

      newfunc.invalidate = invalidate
      return newfunc
    return funcwrap

I am using this on a class method, so something like:

class StorageUnit(models.Model):
  @memoize(timeout=60*180, keygenfunc=lambda x,y: str(x[0].id))
  def someBigCalculation(self):
    ...
    return result

The actual memoization process works perfectly! That is, a call to

myStorageUnitInstance.someBigCalculation()

properly uses the cache. OK, cool!

My problem is when I try to manually invalidate the entry for a specific instance, where I want to be able to run

myStorageUnitInstance.someBigCalculation.invalidate()

However, this doesn’t work, because “self” doesn’t get passed in and therefore the key doesn’t get made. I get a “IndexError: tuple index out of range” error pointing to my lambda function as shown earlier.

Of course, I can successfully call:

myStorageUnitInstance.someBigCalculation.invalidate(myStorageUnitInstance)

and this works perfectly. But it “feels” redundant when I’m already referencing a specific instance. How can I make Python treat this as an instance-bound method and therefore properly fill in the “self” variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:18:00+00:00Added an answer on May 12, 2026 at 9:18 pm

    Descriptors must always be set on the class, not on the instance (see the how-to guide for all details). Of course, in this case you’re not even setting it on the instance, but rather on another function (and fetching it as an attribute of a bound method). I think that the only way to use the syntax you want is to make funcwrap an instance of a custom class (which class must be a descriptor class, of course, i.e., define the appropriate __get__ method, just like functions intrinsically do). Then invalidate can be a method of that class (or, perhaps better, the other custom class whose instance is the “bound-method-like substance” produced by the formerly mentioned descriptor class’s __get__ method), and eventually reach the im_self (that’s how it’s named in a bound method) that you crave.

    A pretty hefty (conceptual and coding;-) price to pay for the minor convenience you seek — hefty enough that I don’t really feel like spending an hour or two developing it completely and testing it. But I hope I’ve given you clear-enough indications for you to proceed if you’re still keen on this, and indeed I’ll be glad to clarify and help out if there’s anything unclear or something is stumping you along this progress.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.