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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:11:09+00:00 2026-05-24T10:11:09+00:00

Possible Duplicate: Why doesn't the weakref work on this bound method? A bit of

  • 0

Possible Duplicate:
Why doesn't the weakref work on this bound method?

A bit of context:

I was trying to implement an Listener (or Observer, same thing) pattern: An EventManager keeps a list of all the Listeners handlers interested in an Event. For example, a Listener object would have a onEndOfTheWorldEvent method which would be called by the EventManager each time an instance of the event class EndOfTheWorldEvent is posted. Easy.

Except that I wanted to weak reference the handlers because I don’t want the EventManager to keep my handlers (bound methods) alive when the Listener was not needed anymore.

So I thought “Let’s throw all the handlers in a WeakSet”. I couldn’t get it to work.

I dump here the code (or what’s left of it when I reduce it to the minimum, here there’s only one type of event and only one type of handler).

#! /usr/bin/python
"""

"""
import sys
import weakref

class Listener(object):
    def handler(self, event):
        print event

class EventManager(object):
    def __init__(self):
        self.handlers = weakref.WeakSet()
    def register(self, listener):
        print "Registering..."
        self.handlers.add(listener.handler)
        CountRefs(listener.handler)
        print "Number of handlers registered:", len(self.handlers)
        print "Registered."

def CountRefs(what):
    print "Hard count:", sys.getrefcount(what)
    print "Weak count:", weakref.getweakrefcount(what)

listener = Listener()
em = EventManager()
CountRefs(listener.handler)
em.register(listener)
CountRefs(listener.handler)

result:

Hard count: 3
Weak count: 0
Registering...
Hard count: 3
Weak count: 0
Number of handlers registered: 0
Registered.
Hard count: 3
Weak count: 0

It just looks like there’s never any weak reference, and the set remains empty.

To make it even simpler:

>>> class C(object):
>>>     def blah(self):
>>>         print "blah"
>>> 
>>> c = C()
>>> w = weakref.ref(c.blah)
>>> print w
<weakref at 0x11e59f0; dead>

Can’t I create weakrefs to methods at all ? If not, why not ?

So I guess a workaround would be to replace the WeakSet with a WeakKeyDictionary: key is the listener itself, and value the handler. Indeed I can weakref my Listeners. But it makes the data structure a bit more complicated, and when comes the time to broadcast the events to everybody, there’s one more level in that structure to go through.

What do you think ?

  • 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-24T10:11:11+00:00Added an answer on May 24, 2026 at 10:11 am

    Let’s say you want weakrefs on a method “meth”.

    You can get weakrefs on it like this

    weak_obj = weakref.ref(meth.im_self)
    weak_func = weakref.ref(meth.im_func)
    

    So, you can deref it like that

    obj = weak_obj()
    func = weak_func()
    

    and get “meth” back with

    meth = getattr(obj, func.__name__)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why doesn't the weakref work on this bound method? I'm using weakrefs
Possible Duplicate: Why does this conversion doesn't work? Hi, i discovered a strange behaviour
Possible Duplicate: JavaScript function aliasing doesn't seem to work Why doesn't this work? function
Possible Duplicate: Python: Adding element to list while iterating This doesn't seem to work,
Possible Duplicate: Conditional operator assignment with Nullable<value> types? Hi, Why this doesn't work? DateTime?
Possible Duplicate: Using var outside of a method I've searched for this a bit,
Possible Duplicate: Why Objective-C doesn't support method overloading? I was going through this link
Possible Duplicate: Question mark equals, doesn't work on php <?php $a=123; ?> <?=$a?> This
Possible Duplicate: problem with template inheritance This code doesn't compile in GCC: template <typename
Possible Duplicate: How does this work? Weird Towers of Hanoi Solution While surfing Google,

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.