Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'explicitly': 0.05; '21,': 0.07; 'skip:u 30': 0.07; '22,': 0.09; 'callback': 0.09; 'caller': 0.09; 'chunk': 0.09; 'function,': 0.09; 'objects,': 0.09; 'themselves,': 0.09; 'yeah,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'gui': 0.12; 'callable': 0.16; 'closure,': 0.16; 'closures': 0.16; 'identifier,': 0.16; 'lambda': 0.16; 'layout,': 0.16; 'objects.': 0.16; 'other,': 0.16; 'problem).': 0.16; 'python),': 0.16; 'screen,': 0.16; 'simplified': 0.16; 'weird.': 0.16; 'window;': 0.16; 'do,': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'library': 0.18; 'trying': 0.19; 'later': 0.20; '(the': 0.22; 'feb': 0.22; 'code,': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'button,': 0.24; 'case.': 0.24; 'form:': 0.24; 'instance,': 0.24; 'integer': 0.24; 'library,': 0.24; 'of.': 0.24; 'skip': 0.24; 'tend': 0.24; 'connected': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'external': 0.29; 'am,': 0.29; 'thus': 0.29; "doesn't": 0.30; 'originally': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'usually': 0.31; 'breaking': 0.31; 'keys': 0.31; 'once,': 0.31; 'allows': 0.31; 'option': 0.32; 'another': 0.32; 'cases': 0.33; 'maybe': 0.34; 'could': 0.34; 'agree': 0.35; 'problem.': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'programming,': 0.36; 'charset:us-ascii': 0.36; 'similar': 0.36; 'so,': 0.37; 'list': 0.37; 'being': 0.38; 'button': 0.38; 'message-id:@gmail.com': 0.38; 'question,': 0.38; 'window': 0.38; 'whatever': 0.38; 'rather': 0.38; 'expect': 0.39; 'does': 0.39; 'either': 0.39; 'how': 0.40; 'even': 0.60; 'remove': 0.60; 'skip:u 10': 0.60; 'event.': 0.60; 'removing': 0.60; 'signal': 0.60; 'length': 0.61; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'back': 0.62; 'making': 0.63; 'header:Message-Id:1': 0.63; 'kind': 0.63; 'more': 0.64; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'hang': 0.67; 'it!': 0.67; 'frequently': 0.68; 'realized': 0.68; 'to,': 0.72; 'click': 0.77; 'subject:Design': 0.78; '2015': 0.84; 'everything.': 0.84; "it'd": 0.84; 'pike': 0.84; 'subject:thought': 0.84; 'info,': 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=Hw0o52CPakQ5EaCDD8+U+Nt5vw9aM5u9/Yd+C+3U+eg=; b=T3ypzf/E+A83Xg90zCT1q9gb2ps7ibq3WBRFH7OBAJkBZ8vkmc/ezdoccNsgLKQIJW G2zwM/Sk5pUi1fpOlVV/+NjaH49l+KEgGpy+lIl+k/XeSMJZTR07ITnQp/g+w4kG2Pu4 KBqLTHaLfzh+EyavFRepUpKrmJmOm3tWTFWu8gJayEYXhdL+2/zgtHiOcP7r9rt67G9Z g+22GW/DpP+B6wwdsYtZq6sczZbch9c/nTsO9s6kM83AERXjRoGCeeS14V5G+4ejJHs/ 4grtnYKC/D02Vsg77MgVdaB9HzHYI5tt55tyCYZYWA0tLYj0erq/Z8A1+SZsGCxGFPpz exYg== X-Received: by 10.140.32.166 with SMTP id h35mr6429343qgh.31.1424533523664; Sat, 21 Feb 2015 07:45:23 -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 10:45:22 -0500 Content-Transfer-Encoding: quoted-printable References: <33677AE8-B2FA-49F9-9304-C8D93784255D@gmail.com> <39813568-6DB8-4341-A130-C256CFF352EE@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: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424533526 news.xs4all.nl 2847 [2001:888:2000:d::a6]:33910 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 9453 X-Received-Body-CRC: 3710368125 Xref: csiph.com comp.lang.python:86049 On Feb 21, 2015, at 9:36 AM, Chris Angelico wrote: > On Sun, Feb 22, 2015 at 1:07 AM, Cem Karan wrote: >> I agree about closures; its the only way they could work. When I was = originally thinking about the library, I was trying to include all types = of callbacks, including closures and callable objects. The callable = objects may pass themselves, or one of their methods to the library, or = may do something really weird. >>=20 >> Although I just realized that closures may cause another problem. In = my code, I expect that many different callbacks can be registered for = the same event. Unregistering means you request to be unregistered for = the event. How do you do that with a closure? Aren't they anonymous? >>=20 >=20 > They're objects, same as any other, so the caller can hang onto a > reference and then say "now remove this one". Simple example: >=20 > callbacks =3D [] > def register_callback(f): callbacks.append(f) > def unregister_callback(f): callbacks.remove(f) > def do_callbacks(): > for f in callbacks: > f() >=20 > def make_callback(i): > def inner(): > print("Callback! %d"%i) > register_callback(inner) > return inner >=20 > make_callback(5) > remove_me =3D make_callback(6) > make_callback(7) > unregister_callback(remove_me) > do_callbacks() Yeah, that's pretty much what I thought you'd have to do, which kind of = defeats the purpose of closures (fire-and-forget things). BUT it does = answer my question, so no complaints about it! So, either you keep a reference to your own closure, which means that = the library doesn't really need to, or the library keeps hold of it for = you, in which case you don't have a reasonable way of removing it. > The other option is for your callback registration to return some kind > of identifier, which can later be used to unregister the callback. > This is a good way of avoiding reference cycles (the ID could be a > simple integer - maybe the length of the list prior to the new > callback being appended, and then the unregistration process is simply > "callbacks[id] =3D None", and you skip the Nones when iterating), and > even allows you to register the exact same function more than once, > for what that's worth. That would work. In the cases where someone might register & unregister = many callbacks, you might use UUIDs as keys instead (avoids the ABA = problem). > When I do GUI programming, this is usually how things work. For > instance, I use GTK2 (though usually with Pike rather than Python), > and I can connect a signal to a callback function. Any given signal > could have multiple callbacks attached to it, so it's similar to your > case. I frequently depend on the GTK engine retaining a reference to > my function (and thus to any data it requires), as I tend not to hang > onto any inner objects that don't need retention. Once the parent > object is destroyed, all its callbacks get dereferenced. Consider this > simplified form: >=20 > def popup_window(): > w =3D Window() > # Add layout, info, whatever it takes > btn =3D Button("Close") > w.add(btn) # actually it'd be added to a layout > btn.signal_connect("clicked", lambda *args: w.destroy()) >=20 > The GUI back end will hang onto a reference to the window, because > it's currently on screen; to the button, because it's attached to the > window; and to my function, because it's connected to a button signal. > Then when you click the button, the window gets destroyed, which > destroys the button, which unregisters all its callbacks. At that > point, there are no refs to the function, so it can get disposed of. > That button function was the last external reference to the window, > and now that it's not on screen, its Python object can also be > disposed of, as can the button inside. So it'll all clean up fairly > nicely; as long as the callback gets explicitly deregistered, that's > the end of everything. OK, so if I'm reading your code correctly, you're breaking the cycle in = your object graph by making the GUI the owner of the callback, correct? = No other chunk of code has a reference to the callback, correct? =20 Thanks, Cem Karan=