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


Groups > comp.lang.python > #86007

Design thought for callbacks

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <cfkaran2@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.073
X-Spam-Evidence '*H*': 0.85; '*S*': 0.00; 'nasty': 0.07; 'bits': 0.09; 'callback': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'python': 0.11; 'creates': 0.14; 'design?': 0.16; 'weird': 0.16; 'thanks,': 0.17; 'code.': 0.18; 'all,': 0.19; 'not,': 0.20; 'earlier': 0.24; 'question': 0.24; 'first,': 0.26; 'certain': 0.27; 'developing': 0.27; 'idea': 0.28; "i'm": 0.30; 'code': 0.31; 'that.': 0.31; 'accidentally': 0.31; 'themselves': 0.32; 'url:python': 0.33; 'everyone': 0.33; 'could': 0.34; 'problem': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'project': 0.37; 'message- id:@gmail.com': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'bad': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'skip:u 10': 0.60; 'worry': 0.60; 'url:3': 0.61; 'header:Message-Id:1': 0.63; 'kept': 0.65; 'subject:Design': 0.78; 'inform': 0.78; '3.4': 0.84; 'about?': 0.84; 'preventing': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'bounce': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:content-type:content-transfer-encoding:subject:message-id:date :to:mime-version; bh=dZ0ufj9ynbtfJx5uIoDygbvs2uiKvdx+/8ONU5cAdro=; b=jsQLGkAt+V01t7Xj7iPckdLW9b1T5ZiAjgMEjQIbdEGeEv7YksH0hRHMIGxkR4K5XU R3RCe5qU9+KKsRQ19WpwR9i/IW7SnQywUk0eHnsCq9MMqKSQZIqFK6Qq7wPbPTESfJSA FO+r8WLubwECKG9NFNYx4vtW+BV9ZAHpSdSVRxHHmRnYzH/h8jSVtpQ/bCYXwghy9xZQ X8uxUfbsAXqevm3A2ShhAdpErtgWzrNQY5fGlcwOIRCVJ1Fp0Epd0WKKm+/PPXco0Vdl Vpraq7Wb8Tg1cbEwwVpIYKkEnt1k2q0qjAlAwQLaJP+DcmAXD4s1yzM5eyCkZm5791Sm KqPw==
X-Received by 10.140.150.146 with SMTP id 140mr1737836qhw.66.1424486667482; Fri, 20 Feb 2015 18:44:27 -0800 (PST)
From Cem Karan <cfkaran2@gmail.com>
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding quoted-printable
Subject Design thought for callbacks
Date Fri, 20 Feb 2015 21:44:25 -0500
To "comp.lang.python" <python-list@python.org>
Mime-Version 1.0 (Mac OS X Mail 6.6 \(1510\))
X-Mailer Apple Mail (2.1510)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18942.1424486670.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424486670 news.xs4all.nl 2918 [2001:888:2000:d::a6]:40162
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86007

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web