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

  • SEARCH
  • Home
  • 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 654537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:30:19+00:00 2026-05-13T22:30:19+00:00

Here is my problem. I have created a pretty heavy readonly class making many

  • 0

Here is my problem. I have created a pretty heavy readonly class making many database calls with a static “factory” method. The goal of this method is to avoid killing the database by looking in a pool of already-created objects if an identical instance of the same object (same type, same init parameters) already exists.

If something was found, the method will just return it. No problem. But if not, how may I create an instance of the object, in a way that works with inheritance?

>>> class A(Object):
>>>     @classmethod
>>>     def get_cached_obj(self, some_identifier):
>>>         # Should do something like `return A(idenfier)`, but in a way that works

>>> class B(A):
>>>     pass

>>> A.get_cached_obj('foo') # Should do the same as A('foo')
>>> A().get_cached_obj('foo') # Should do the same as A('foo')
>>> B.get_cached_obj('bar') # Should do the same as B('bar')
>>> B().get_cached_obj('bar') # Should do the same as B('bar')

Thanks.

  • 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-13T22:30:19+00:00Added an answer on May 13, 2026 at 10:30 pm
    import weakref
    
    class A(object):
      _get_obj_cache = weakref.WeakValueDictionary()
      @classmethod
      def get_obj(cls, identifier):
        cache = cls._get_obj_cache
        obj = cache.get((cls, identifier))
        if obj is None:
          obj = cache[(cls, identifier)] = cls(identifier)
        return obj
    
    class B(A):
      pass
    

    Because a WeakValueDictionary is used, the objects will remain cached as long as you have any other reference to them, and you can call SomeClass.get_obj(identifier) as many times as you like to get that same object. If I’ve understood you correctly, it’s the cls(identifier) which will hit the database and that’s what you want to call less frequently (since you know the objects are immutable.)

    If you want to keep objects in the cache even if they are no longer referenced elsewhere, then change the WeakValueDictionary into a normal dict.

    This requires that identifier is suitable for a dict key, and if it’s a string (as you have in your example code) then it is.

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

Sidebar

Related Questions

Pretty basic problem here. So I have a Java package that I have created
I have created a jsfiddle of my problem. Here is a link to the
Here is my problem: I have an array of model class(Let's say, 'addressModel' with
I have what I consider to be a pretty unique problem here, and no
I have created a custom even class which is pretty basic. But when calling
I am having a problem making a window appear programmatically..... I have created the
Here's my problem: I have do create a menu/list of actions (which would be
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have
Here is the problem: I have two columns in a table that, for each

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.