Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '21,': 0.07; 'attribute': 0.07; 'nasty': 0.07; 'bits': 0.09; 'callback': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'references.': 0.09; 'cc:addr:python- list': 0.11; 'creates': 0.14; 'called.': 0.16; 'closure,': 0.16; 'expect,': 0.16; 'unlikely': 0.16; 'sat,': 0.16; 'weird': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'library': 0.18; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'first,': 0.26; 'query': 0.26; 'references': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; 'themselves': 0.32; '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; 'done': 0.36; 'charset:us-ascii': 0.36; 'so,': 0.37; 'message-id:@gmail.com': 0.38; 'problems': 0.38; 'handle': 0.38; 'pm,': 0.38; 'expect': 0.39; 'system.': 0.39; 'users': 0.40; 'how': 0.40; 'ensure': 0.60; 'skip:u 10': 0.60; 'header:Message- Id:1': 0.63; 'hang': 0.67; 'fact,': 0.69; 'subject:Design': 0.78; 'inform': 0.78; '2015': 0.84; 'loose': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'careful': 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=eXpxU+txXDI3W0Wn89iq2e6nZblI4AwMABiWzygktvc=; b=WvdFzQuaib2kJwZrPGkpmbG9vaN/RlqAqI7Mg5gJRrwfBxRDKIZ3fN3mORWEJS0j/x g4YM5NNywifA1o6ryvVk49obmEjdMLhCpLS7LvM+RFiCWBc3Trp0lrVF44S5Sy3RF5gK asVdzby7yHjdpNYqX4aXoFGyYim+XpyKjKu01Aqy5GNq8XkL9qDIYna+98nsPTbrwF/7 2VVOzgkY02wO+yZUQaY12ed0gLEonSehqLFYFGsgvb0GP98alWmf3FG8F6XQVdti440g dL07TTDtE2pIru65Nk+PCcCLBov9l3iVmddO2nxCNjznj4YZTuy1k00yN+FvYMjrv7DU WRmg== X-Received: by 10.52.12.138 with SMTP id y10mr6048781vdb.35.1424607032687; Sun, 22 Feb 2015 04:10:32 -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: <87zj87i74p.fsf@elektro.pacujo.net> Date: Sun, 22 Feb 2015 07:10:31 -0500 Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <87zj87i74p.fsf@elektro.pacujo.net> To: Marko Rauhamaa 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424607035 news.xs4all.nl 2870 [2001:888:2000:d::a6]:54820 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86101 On Feb 21, 2015, at 11:03 AM, Marko Rauhamaa wrote: > Chris Angelico : >=20 >> On Sat, Feb 21, 2015 at 1:44 PM, Cem Karan = wrote: >=20 >>> 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, >>> [...] >>=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 > I use callbacks all the time but haven't had any problems with strong > references. >=20 > I am careful to move my objects to a zombie state after they're done = so > they can absorb any potential loose callbacks that are lingering in = the > system. So, if I were designing a library for you, you would be willing to have = a 'zombie' attribute on your callback, correct? This would allow the = library to query its callbacks to ensure that only 'live' callbacks are = called. How would you handle closures? =20 Thanks, Cem Karan=