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 8497655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:11:17+00:00 2026-06-11T00:11:17+00:00

I am using function objects as dictionary keys. I am doing that because I

  • 0

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).

  • 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-06-11T00:11:19+00:00Added an answer on June 11, 2026 at 12:11 am

    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 …)

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

Sidebar

Related Questions

I agree that Functions are objects in JS. When using a Function as a
I am using these two functions in order to rotate my objects in my
I've read that using Object.prototype to attach functions to custom JavaScript objects is more
I know it is possible to create classes/objects in Perl using the bless function
if in a literal object i try to reference a function using this inside
I'm using a Dictionary with weak keys to test if an object has been
I find myself in a need of working with functions and objects who take
I am having trouble building a Python function that launches TkInter objects, with commands
I made a static function that returns me an ArrayList of objects: allThread =(ArrayList)
This is a serializer I've been working on that serializes datarow's into objects based

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.