Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24979

Re: Question about weakref

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'class,': 0.07; 'subject:Question': 0.07; 'though:': 0.07; 'python': 0.09; 'garbage': 0.09; 'referenced': 0.09; 'weak': 0.09; 'cc:addr :python-list': 0.10; 'itself.': 0.11; "'b'": 0.16; '__del__': 0.16; 'foo()': 0.16; 'foo(object):': 0.16; 'order?': 0.16; 'posted,': 0.16; 'proxy,': 0.16; 'quite.': 0.16; 'reproduce': 0.16; 'sure.': 0.16; 'using,': 0.16; 'weakref': 0.16; 'wrote:': 0.17; 'deleted.': 0.17; 'refers': 0.17; '>>>': 0.18; 'bit': 0.21; 'import': 0.21; 'object.': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'example': 0.23; 'cc:no real name:2**0': 0.24; 'pass': 0.25; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'separate': 0.27; 'object,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'class': 0.29; "i'm": 0.29; 'fri,': 0.30; 'code': 0.31; 'point': 0.31; 'generally': 0.32; 'cases,': 0.33; 'problem': 0.33; 'presence': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'false': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; "wasn't": 0.36; 'method': 0.36; 'possible': 0.37; 'does': 0.37; 'uses': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'sure': 0.38; 'delete': 0.38; 'build': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'remove': 0.61; 'story': 0.61; 'more': 0.63; 'difficulty': 0.65; 'jul': 0.65; 'reverse': 0.65; 'fact,': 0.69; 'further,': 0.71; 'frank': 0.75; '1:00': 0.84; 'investigated': 0.84; 'listener': 0.84; 'locally': 0.84
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 :cc:content-type; bh=mXnQVFL2UsExYaoelaFP6rpPdUt6BqCtnwWVFcgoFTY=; b=fqWd0O2oSwJxWrglnrN2o9QJ01O2aA7zZcZxxhCoWhZUNIvoSjdgpiNHdxJ3lQcRDf OIMv4mabB0II8nx3jN96SyH8yrGzM2GwskggnU3//HkSN+MHwqv0t8R7b9vkVCX2aRr9 yMlxjoPKvQn+ub2Jh/vTSu1jyZioSQQWvUFXqonaha0LPOB8IQ6GcNeu5uxACTNCg2Wo Xv46drF4mS6SuE5EME+kVytbmcZyrcQGx0kK6MffRsNaqI84tR9jUVaFIQxzQhPBv4jk VZ3otyFdlBC18gJnNb5l7NMfOK0Q3p6VXsdLW9q0OndIpuG5djtNvfU+ddEBSWsm/KyE W4UA==
MIME-Version 1.0
In-Reply-To <jt62f2$t89$1@dough.gmane.org>
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 Fri, 6 Jul 2012 11:04:06 -0600
Subject Re: Question about weakref
To Frank Millman <frank@chagford.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1865.1341594278.4697.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341594278 news.xs4all.nl 6988 [2001:888:2000:d::a6]:55942
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24979

Show key headers only | View raw


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


Thread

Re: Question about weakref Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-06 11:04 -0600

csiph-web