Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #24979
| References | <jt1hg2$bmp$1@dough.gmane.org> <87hatmr3xf.fsf@handshake.de> <jt3tc0$5q6$1@dough.gmane.org> <87a9ze85h2.fsf@handshake.de> <jt62f2$t89$1@dough.gmane.org> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-07-06 11:04 -0600 |
| Subject | Re: Question about weakref |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1865.1341594278.4697.python-list@python.org> (permalink) |
On Fri, Jul 6, 2012 at 1:00 AM, Frank Millman <frank@chagford.com> wrote: > I have investigated a bit further, and now I have a clue as to what is > happening, though not a full understanding. > > If you use 'b = weakref.ref(obj)', 'b' refers to the weak reference, and > 'b()' refers to the referenced object. > > If you use 'b = weakref.proxy(obj)', 'b' refers to the referenced object. I > don't know how to refer to the weak reference itself. In a way that is the > whole point of using 'proxy', but the difficulty comes when you want to > remove the weak reference when the referenced object is deleted. Not quite. 'b' refers to the proxy, which uses magic methods to mimic the referenced object. It is still a separate object, however. In fact, I actually think it's not directly possible to refer to the *referenced object* via a proxy, although there are round-about ways to accomplish it. >>> import weakref >>> class Foo(object): pass >>> a = Foo() >>> id(a) 11253072 >>> b = weakref.proxy(a) >>> id(b) 11258400 >>> a is a True >>> a is b False > The full story is more complicated than that - why does my example work when > I delete x, then y, then z, but not if I reverse the order? On that, I'm really not sure. I tried to reproduce the problem locally and wasn't able to. What build of Python are you using, and on what platform? I have one suggestion, though: you might try removing the __del__ method from the listener class, as the presence of that method can interfere with garbage collection in some cases, and it is generally contra-recommended. I'm not sure why that would affect the code you posted, but it can't hurt to try it. Cheers, Ian
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Question about weakref Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-06 11:04 -0600
csiph-web