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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:58:48+00:00 2026-05-23T13:58:48+00:00

I have a situation in which I’d like to maintain a mapping from one

  • 0

I have a situation in which I’d like to maintain a mapping from one object to another for as long as the first object exists. My first thought was to use a WeakKeyDictionary.

import weakref
import gc

class M:
    pass

w = weakref.WeakKeyDictionary()
m = M()
w[m] = some_other_object
del m
gc.collect()
print w.keys()

This works fine in most cases. However, in the case where some_other_object is (or has a reference to) m, the M instance will not be garbage collected. (To see an example, replace some_other_object with m)

Of course, if I stored the mapping on the object itself, it would be garbage collected when I deleted it:

import weakref
import gc

class M:
    pass

m = M()
m.circular_reference = m
r = weakref.ref(m)
del m
gc.collect()
print r

Can I achieve the results of the second example using the weakref module (i.e. without mutating m)?

In other words, can I use the weakref module to map an object to itself (or another object that has a strong reference to it) and only keep the object in memory as long as there are other references to it?

  • 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-23T13:58:49+00:00Added an answer on May 23, 2026 at 1:58 pm

    You don’t really have circular references in these examples. Your circle goes like:

    WeakKeyDict -> value -> key -> …

    So the dict keeps the value alive, which in turn keeps the key alive, and through a separate mechanism that does not involve a strong reference, the key instructs the dict to keep the value alive. Since there is no proper reference circle here, the GC never detects any issue. (However, your second example does contain a circular reference, which is why it behaves like the first)

    I suspect the only solution to your problem is to ensure that the values of the dict never have strong references (direct or indirect) to any of the keys in the dict. This is similar to what srgerg proposed, but rather than making weak references to all values, you really want references to the keys to be weak. For example:

    import weakref
    import gc
    
    class M:
        pass
    
    w = weakref.WeakKeyDictionary()
    key = M()
    val = M()
    val.keyRef = weakref.ref(val)
    w[key] = val
    print len(w)   ## prints 1
    del key
    gc.collect()
    print len(w)   ## prints 0
    

    This way, there are always strong references to the values, but you are carefully controlling references to keys to determine what gets removed from dict. Depending on the complexity of your program, this could be time consuming to implement, since you need to manually track down all references to keys.

    But perhaps we can propose a simpler solution if you tell us more about the specific problem..

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

Sidebar

Related Questions

I have situation in which I read a record from a database. And if
I have a situation in which I need to extract Data Annotations information from
I have a situation in which a file exists in both the repo and
I have a situation in which my Java project contains resources from a different
I have a situation in which a large amount of data output from Matlab
I have a situation in which text in one of the cells is massive.When
I have a situation in which a managed DLL calls some unmanaged DLL. I
I have a situation in which I need to convert a text data into
I have a situation in which user can single tap a control, which show
I have a situation in which the ideal relationship, I believe, would involve Value

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.