Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.019 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '21,': 0.07; 'nasty': 0.07; 'bits': 0.09; 'callback': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'cc:addr:python-list': 0.11; 'creates': 0.14; 'callable': 0.16; 'closure,': 0.16; 'design?': 0.16; 'expect,': 0.16; 'garbage': 0.16; 'surprises': 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; 'library': 0.18; 'possible,': 0.19; 'not,': 0.20; 'feb': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'earlier': 0.24; 'question': 0.24; 'cc:2**0': 0.24; '(see': 0.26; 'first,': 0.26; 'references': 0.26; 'least': 0.26; 'certain': 0.27; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; 'accidentally': 0.31; 'end,': 0.31; 'themselves': 0.32; 'url:python': 0.33; 'not.': 0.33; 'could': 0.34; 'problem': 0.35; 'no,': 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'message- id:@gmail.com': 0.38; 'url:library': 0.38; 'list,': 0.38; 'pm,': 0.38; 'expect': 0.39; 'users': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'url:3': 0.61; 'matter': 0.61; 'you.': 0.62; 'back': 0.62; 'header:Message-Id:1': 0.63; 'kept': 0.65; 'to:addr:gmail.com': 0.65; 'hang': 0.67; 'fact,': 0.69; 'surprise': 0.74; 'subject:Design': 0.78; 'inform': 0.78; "'dead'": 0.84; '2015': 0.84; 'about?': 0.84; 'grave': 0.84; 'haunt': 0.84; 'preventing': 0.84; 'situations,': 0.84; 'subject:thought': 0.84 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=74x5eVdfbnA6G3YQ2ApxyUbD2GjSjJ1neJbJT8PgrNQ=; b=bvTzpJTM0lTZjeqkbTVp3qhRYaCaGhtXXYm4xEs76Wzu+5zxbDF/C+1CNQx2xXLdNW Fq6bTFV+eptE9k7RsB11cL34c8a4uhphYaoM1HGjf/EgqHTXgVtTqCkd9Pk9Ed7If6Ft qeE3rFo9JwCK0xM4a1jg3Ac6Nn1W/NhKtoK5mrc3RKkVsvFACr1rh5eJDveipHHzXsso J4leFdyDL+yLg9NWzOvoSGxEcMF5OtCB6h+rjRpIkOHVJHPjh1k0vzw/7HfCfL/TTLLW yPXKnIOsD38QUJ8HeTTQP4IkZqn3ghCpd/VbtEPWu0AwzfkEd8jZtpUOGvvjxTrDyUeW 69Lw== X-Received: by 10.140.36.239 with SMTP id p102mr5377500qgp.44.1424524389503; Sat, 21 Feb 2015 05:13:09 -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: Sat, 21 Feb 2015 08:13:08 -0500 Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> To: Chris Angelico X-Mailer: Apple Mail (2.1510) Cc: "comp.lang.python" 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424524392 news.xs4all.nl 2864 [2001:888:2000:d::a6]:52170 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86028 On Feb 21, 2015, at 12:42 AM, Chris Angelico wrote: > 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. OK, so it would violate the principle of least surprise for you. = Interesting. Is this a general pattern in python? That is, callbacks = are owned by what they are registered with? In the end, I want to make a library that offers as few surprises to the = user as possible, and no matter how I think about callbacks, they are = surprising to me. If callbacks are strongly-held, then calling 'del = foo' on a callable object may not make it go away, which can lead to = weird and nasty situations. Weakly-held callbacks mean that I (as the = programmer), know that objects will go away after the next garbage = collection (see Frank's earlier message), so I don't get 'dead' = callbacks coming back from the grave to haunt me. So, what's the consensus on the list, strongly-held callbacks, or = weakly-held ones? Thanks, Cem Karan=