Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'tree': 0.05; '21,': 0.07; 'nasty': 0.07; 'result,': 0.07; 'bits': 0.09; 'callback': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'creates': 0.14; 'closure,': 0.16; 'design?': 0.16; 'expect,': 0.16; 'expecting': 0.16; 'objects.': 0.16; 'owner.': 0.16; 'preserve': 0.16; 'surprising': 0.16; 'unlikely': 0.16; 'with?': 0.16; 'sat,': 0.16; 'weird': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'not,': 0.20; 'feb': 0.22; '>>>': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'instance,': 0.24; 'refers': 0.24; 'string,': 0.24; 'tend': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'first,': 0.26; 'references': 0.26; 'pass': 0.26; 'least': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; '>>>>': 0.31; 'accidentally': 0.31; 'etc.).': 0.31; 'languages': 0.32; 'themselves': 0.32; 'url:python': 0.33; 'not.': 0.33; 'could': 0.34; 'problem': 0.35; 'no,': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'object,': 0.36; 'done': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'too': 0.37; 'message- id:@gmail.com': 0.38; 'url:library': 0.38; 'pm,': 0.38; 'track': 0.38; 'expect': 0.39; 'structure': 0.39; 'delete': 0.39; 'sure': 0.39; 'users': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'hope': 0.61; 'url:3': 0.61; 'matter': 0.61; "you're": 0.61; 'you.': 0.62; 'header:Message-Id:1': 0.63; 'kept': 0.65; 'hang': 0.67; 'fact,': 0.69; 'surprise': 0.74; 'subject:Design': 0.78; 'inform': 0.78; 'you:': 0.81; '2015': 0.84; 'about?': 0.84; 'dag': 0.84; 'float,': 0.84; 'preventing': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'edwards': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=tW/Atph3eMq6spNVjdENeoZaAbWCvtthqoenxI0/Qvc=; b=rnQejGKTxbBVXBGDW2LxESaOkTlQhhRTSW8PrViOIMk8d9evVdwbSm6adB5AsLkA6q mIitVnMORjXS9DNzuSd2v3YQNQRN2El69HwYIyRzu13pJWtwIOilK9goFcNxfaK4htq1 lUjBC0AtdN/xeQMlGKdjCOB63kCHOW3IRRBL0FGljHRBxpe4XtRj2Q6YM8WdgHkrq3eR G1tHXelaAg47YhwCOsz7f8SbZtsEZ3kq0z/Sv2SRkKFZ1EwfWBvEZ+dlZc5QGV4GXJxC tc5+YY2q10yQpFi9vmbhhzHYob2gJBWWXeO15SDbuNkSQbTDPd52cCwiFP7a0h8xSz2k WEvA== X-Received: by 10.52.118.9 with SMTP id ki9mr6371374vdb.11.1424611296165; Sun, 22 Feb 2015 05:21:36 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) Subject: Re: Design thought for callbacks From: Cem Karan In-Reply-To: Date: Sun, 22 Feb 2015 08:21:35 -0500 Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> To: Grant Edwards X-Mailer: Apple Mail (2.1510) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424611304 news.xs4all.nl 2843 [2001:888:2000:d::a6]:54641 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86111 On Feb 21, 2015, at 3:57 PM, Grant Edwards = wrote: > On 2015-02-21, Cem Karan wrote: >>=20 >> On Feb 21, 2015, at 12:42 AM, Chris Angelico = wrote: >>=20 >>> On Sat, Feb 21, 2015 at 1:44 PM, Cem Karan = wrote: >>>> 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? >>>>=20 >>>=20 >>> No, it's not. I would advise using strong references - if the = callback >>> is a closure, for instance, you need to hang onto it, because there >>> are unlikely to be any other references to it. If I register a >>> callback with you, I expect it to be called; I expect, in fact, that >>> that *will* keep my object alive. >>=20 >> OK, so it would violate the principle of least surprise for you. >=20 > And me as well. I would expect to be able to pass a closure as a > callback and not have to keep a reference to it. Perhaps that just a > leftover from working with other languages (javascript, scheme, etc.). > It doesn't matter if it's a string, a float, a callback, a graphic or > whatever: if I pass your function/library an object, I expect _you_ to > keep track of it until you're done with it. >=20 >> Interesting. Is this a general pattern in python? That is, >> callbacks are owned by what they are registered with? >=20 > I'm not sure what you mean by "owned" or why it matters that it's a > callback: it's an object that was passed to you: you need to hold onto > a reference to it until you're done with it, and the polite thing to > do is to delete references to it when you're done with it. I tend to structure my code as a tree or DAG of objects. The owner = refers to the owned object, but the owned object has no reference to its = owner. With callbacks, you get cycles, where the owned owns the owner. = As a result, if you forget where your object has been registered, it may = be kept alive when you aren't expecting it. My hope was that with = WeakSets I could continue to preserve the DAG or tree while still having = the benefits of callbacks. However, it looks like that is too = surprising to most people. Thanks, Cem Karan=