2

I am using function objects as dictionary keys. I am doing that because I need to cache the result of these functions. This is roughly the code I am using:

# module cache.py:
calculation_cache = {}
def cached(func):
  # func takes input as argument
  def new_function(input):
    try:
      result = calculation_cache[(func, input)]
    except KeyError:
      result = func(input)
      calculation_cache[(func, input)] = result
    return result
  return new_function

# module stuff.py
@cached
def func(s):
  # do something time-consuming to s
  # ...
  return result

I could use func.__module__ + func.__name__ instead of func, but if func works fine, I'd rather use it since I'm afraid of possible name conflicts (e.g., for lambda functions or nested functions or functions that were replaced by another with the same name, etc.)

This seems to work fine, but I suspect that this may cause problems in some hard-to-test situations.

For example, I am concerned about a function being somehow deleted and another function reusing its memory space. In this case, my cache would be invalid, but it would not know about it. Is this a valid concern? If so, is there any way to address it?

Can a function be deleted? Does reloading a module move a function to a new address (thus changing its hash value, and releasing the old memory address for new functions)? Can someone (for some strange reason) simply delete a function defined in a module (again making the memory available for a new function)?

If it is only safe to do this with functions explicitly defined with def, then I can prohibit the use of cached except as a decorator (I don't know how to enforce it, but I can just document it in the cached docstring).

2
  • Functions in python are objects just like everything else. I would suspect it'll stick around for as long as you hold a reference to it (as the keys in your dictionary). I'm not exactly sure the effect reloading would have, but I would assume it creates new function objects. Commented Sep 7, 2012 at 4:31
  • yes reloading created new objects. Commented Sep 8, 2012 at 0:52

2 Answers 2

3

I'm not sure that I can address all of your concerns above, but I can address 1 of them --

I don't see any reason why a function couldn't be garbage collected. However, since your function is a key in a dictionary, as long as that dictionary is around, the reference count for your function will never reach zero and it won't be subject to garbage collection.

I don't know about reloading of a module, however, that seems like a corner case that you shouldn't really need to worry about. Modules aren't really meant to be reloaded ... the fact that you can do it in some circumstances is mostly for debugging purposes in an interactive terminal and not meant to be used in any real code ... (as far as I know anyway ...)

Sign up to request clarification or add additional context in comments.

3 Comments

I didn't think hashing a function value would create a reference to it since hash(f) is just a number. I forgot that the dictionary needs not only the hash but also the reference to the object itself, in order to check if it's really there. Thank you! Seems like it solves my main problem: a function won't be deleted on me. As to reloading of modules, there are some rare use cases. Still, it seems I don't need to worry about this.
@max, a reference is also kept around so you can get the value of the keys (with .keys, .items, etc)
@max -- Yeah, just think about yourdict.keys() and you realize immediately that the dictionary must actually store a reference. I suppose it could do that with a weakref (but it doesn't) because in the event of hash collisions, it needs to have the object as well.
1

Your code should work. Like other have said since the function object is still referenced by the dict, it won't get garbage collected. The function can be delete or the module reloaded. This means the func label will reference a new version of function object (or nothing). But the old function object will still be in memory even it is no longer referred by func. You dictionary will have a separate entry for the old and new function, which may work better for you since the it means you won't get staled result attaching to the old function. You can even use your dictionary as the backdoor to retrieve the old version of function. Just enumerate calculation_cache.keys() will get it back.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.