Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86007 > unrolled thread

Design thought for callbacks

Started byCem Karan <cfkaran2@gmail.com>
First post2015-02-20 21:44 -0500
Last post2015-02-20 21:44 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Design thought for callbacks Cem Karan <cfkaran2@gmail.com> - 2015-02-20 21:44 -0500

#86007 — Design thought for callbacks

FromCem Karan <cfkaran2@gmail.com>
Date2015-02-20 21:44 -0500
SubjectDesign thought for callbacks
Message-ID<mailman.18942.1424486670.18130.python-list@python.org>
Hi all, I'm working on a project that will involve the use of callbacks, and I want to bounce an idea I had off of everyone to make sure I'm not developing a bad idea.  Note that this is for python 3.4 code; I don't need to worry about any version of python earlier than that.

In order to inform users that certain bits of state have changed, I require them to register a callback with my code.  The problem is that when I store these callbacks, it naturally creates a strong reference to the objects, which means that if they are deleted without unregistering themselves first, my code will keep the callbacks alive.  Since this could lead to really weird and nasty situations, I would like to store all the callbacks in a WeakSet (https://docs.python.org/3/library/weakref.html#weakref.WeakSet).  That way, my code isn't the reason why the objects are kept alive, and if they are no longer alive, they are automatically removed from the WeakSet, preventing me from accidentally calling them when they are dead.  My question is simple; is this a good design?  If not, why not?  Are there any potential 'gotchas' I should be worried about?

Thanks,
Cem Karan

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web