Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.045 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; '21,': 0.07; 'nasty': 0.07; 'bits': 0.09; 'callback': 0.09; 'messing': 0.09; 'naturally': 0.09; 'objects,': 0.09; 'preferable': 0.09; 'creates': 0.14; 'cleaned': 0.16; 'closure,': 0.16; 'design?': 0.16; 'expect,': 0.16; 'losing': 0.16; 'unlikely': 0.16; 'with?': 0.16; 'sat,': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'not,': 0.20; 'feb': 0.22; '>>>': 0.22; 'memory': 0.22; 'python?': 0.22; 'instance,': 0.24; 'string,': 0.24; 'question': 0.24; 'first,': 0.26; 'references': 0.26; 'pass': 0.26; 'least': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 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; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'track': 0.38; 'rather': 0.38; 'expect': 0.39; 'delete': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'users': 0.40; 'ensure': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'url:3': 0.61; 'matter': 0.61; "you're": 0.61; 'you.': 0.62; 'kept': 0.65; 'relatively': 0.65; 'spot': 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; 'float,': 0.84; 'leak': 0.84; 'leak.': 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=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=z7JSkhtPVqzQR+dGHw/aWRoEftILXDr8zT7ow6PYU4I=; b=IkMFV0IA4pvKFnLJUO4Nxq3WL/estSFQgt5BzQonSmEkFO+s0N5y22V+FZfO3XNX1t kKfMn/WvT1keAcVVuSq2O+UX8FhFZe11eGmzc6lzNrxQzXTMoShFp1L/lKcFjrl39eoa Y5FSkoL/QANWCxacvOcPV60gZD665ltvTOt+mKddn2RH9SxJbAk/l9asTnC1c5UL26F3 cZ7YiPqsIOSV9a83SX3A+TdC+MvC8W6iyXaHNNaOgOlquUQ2lbbSLhHqNO2S5CfyidCK f3g73pYZGMvwiPKXRE6WEkNvC7KCbe3Vpot3jx9T1emBnxn9nPAgMqOgpyuM0kYZaX2n KHSw== X-Received: by 10.68.237.2 with SMTP id uy2mr9716976pbc.72.1424588758540; Sat, 21 Feb 2015 23:05:58 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> From: Ian Kelly Date: Sun, 22 Feb 2015 00:05:18 -0700 Subject: Re: Design thought for callbacks To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424588761 news.xs4all.nl 2853 [2001:888:2000:d::a6]:49652 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:86084 On Sat, Feb 21, 2015 at 1:57 PM, Grant Edwards wr= ote: > On 2015-02-21, Cem Karan wrote: >> >> 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 re= quire 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 obje= cts, which means that if they are deleted without unregistering themselves = first, my code will keep the callbacks alive. Since this could lead to rea= lly 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 quest= ion is simple; is this a good design? If not, why not? Are there any pote= ntial 'gotchas' I should be worried about? >>>> >>> >>> 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. > > 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. > >> Interesting. Is this a general pattern in python? That is, >> callbacks are owned by what they are registered with? > > 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. > >> So, what's the consensus on the list, strongly-held callbacks, or >> weakly-held ones? Count me in the weak-ref crowd. It may be a nuisance to keep a reference around on the object registering the callback, but it's preferable to the alternative of messing around with disposables in order to ensure that the callback gets cleaned up and doesn't create a memory leak. I would also rather have my code fail by losing a callback reference, which should be relatively easy to spot and diagnose, than to have said memory leak go unnoticed.