Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.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.015 X-Spam-Evidence: '*H*': 0.97; '*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; 'closure,': 0.16; 'design?': 0.16; 'expect,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'unlikely': 0.16; 'sat,': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'not,': 0.20; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'references': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'accidentally': 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; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'pm,': 0.38; 'expect': 0.39; 'users': 0.40; 'skip:u 10': 0.60; 'url:3': 0.61; 'kept': 0.65; 'hang': 0.67; 'fact,': 0.69; 'subject:Design': 0.78; 'inform': 0.78; '2015': 0.84; 'about?': 0.84; 'preventing': 0.84; 'situations,': 0.84; 'subject:thought': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=23fZGU4zqRLhNKHFS09gLGk3grSTR+5LufZYUPJ90lc=; b=xcKt7uyYybS/CqtS9xn5PpqNMFMDEKugZj3q51n11OhIaY7dbsc6wVMBcfRY8ZKJmg wiE1GrFa5QwrywI72sREAIdRyS+eNfxn+WXXrzNRV4uB7NwryfJTA2zggRV09vmI8sUf LuVFfwLCFBMQDImlfEzOxjvMw26GfmLE4QV7/0Ll7jcIE67lHouxqpoyf7DV96luykmS KAjmRi4lsKCv3LJhQrCy4xEKbHms8Ygx+SGP0z0zNgB54TvgIPX6DNBSxTdqwr8P0Vmr iPpEkL0uG4QSHhDLl1N6oodiGFPZvVQApdZR9+U+G9Ch4JmZdYCcbjOzRsvIYENmKHrc qtDQ== MIME-Version: 1.0 X-Received: by 10.42.13.193 with SMTP id e1mr1434467ica.59.1424497378897; Fri, 20 Feb 2015 21:42:58 -0800 (PST) In-Reply-To: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> Date: Sat, 21 Feb 2015 16:42:58 +1100 Subject: Re: Design thought for callbacks From: Chris Angelico Cc: "comp.lang.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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424497381 news.xs4all.nl 2954 [2001:888:2000:d::a6]:44314 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86016 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 requi= re them to register a callback with my code. The problem is that when I st= ore these callbacks, it naturally creates a strong reference to the objects= , which means that if they are deleted without unregistering themselves fir= st, 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 W= eakSet (https://docs.python.org/3/library/weakref.html#weakref.WeakSet). T= hat way, my code isn't the reason why the objects are kept alive, and if th= ey are no longer alive, they are automatically removed from the WeakSet, pr= eventing 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 potenti= al '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. ChrisA