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


Groups > comp.lang.python > #24959

Re: Question about weakref

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'none,': 0.05; 'removes': 0.05; 'exception.': 0.07; 'parameter': 0.07; 'raised': 0.07; 'subject:Question': 0.07; '"if': 0.09; 'callback': 0.09; 'exist.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'referenced': 0.09; 'weak': 0.09; 'itself.': 0.11; 'times,': 0.13; "'b'": 0.16; 'googled': 0.16; 'message-id:@dough.gmane.org': 0.16; 'order?': 0.16; 're-read': 0.16; 'reason.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'weakref': 0.16; 'wrote:': 0.17; 'deleted.': 0.17; 'refers': 0.17; 'bit': 0.21; 'exists.': 0.22; 'object.': 0.22; 'example': 0.23; 'raise': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'appreciated.': 0.26; 'question': 0.27; 'object,': 0.27; 'header:X -Complaints-To:1': 0.28; 'thus,': 0.29; 'writes:': 0.29; 'manual': 0.29; 'returned': 0.30; 'point': 0.31; 'comments': 0.33; 'to:addr :python-list': 0.33; 'same.': 0.35; 'similar': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'received:co.za': 0.37; 'received:za': 0.37; 'why': 0.37; 'passed': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'delete': 0.38; 'several': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; 'remove': 0.61; 'story': 0.61; 'latest': 0.61; 'more': 0.63; 'difficulty': 0.65; 'reverse': 0.65; 'fact,': 0.69; 'further,': 0.71; 'received:41': 0.73; 'frank': 0.75; 'investigated': 0.84; 'isolated': 0.84; 'findings': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Frank Millman <frank@chagford.com>
Subject Re: Question about weakref
Date Fri, 06 Jul 2012 09:00:49 +0200
References <jt1hg2$bmp$1@dough.gmane.org> <87hatmr3xf.fsf@handshake.de> <jt3tc0$5q6$1@dough.gmane.org> <87a9ze85h2.fsf@handshake.de>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 41-133-114-140.dsl.mweb.co.za
User-Agent Mozilla/5.0 (Windows NT 5.2; rv:12.0) Gecko/20120428 Thunderbird/12.0.1
In-Reply-To <87a9ze85h2.fsf@handshake.de>
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.1853.1341558065.4697.python-list@python.org> (permalink)
Lines 58
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341558065 news.xs4all.nl 6857 [2001:888:2000:d::a6]:57267
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24959

Show key headers only | View raw


On 05/07/2012 19:47, Dieter Maurer wrote:
> Frank Millman<frank@chagford.com>  writes:
>
>> I would still like to know why weakref.proxy raised an exception. I
>> have re-read the manual several times, and googled for similar
>> problems, but am none the wiser.
>
> In fact, it is documented. Accessing a proxy will raise an exception
> when the proxied object no longer exists.
>
> What you can ask is why your proxy has been accessed after the
> object was deleted. The documentation is specific: during the callback,
> the object should still exist. Thus, apparently, one of your proxies
> outlived an event that should have deleted it (probably a hole in
> your logic).
>

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.

This is from the manual section on weakref.ref -
"If callback is provided and not None, and the returned weakref object 
is still alive, the callback will be called when the object is about to 
be finalized; the weak reference object will be passed as the only 
parameter to the callback; the referent will no longer be available."

My callback method looks like this -
     del del_b(b):
        self.array.remove(b)
It successfully removes the weak reference from self.array.

This is from the manual section on weakref.proxy -
"callback is the same as the parameter of the same name to the ref() 
function."

My callback method looks the same. However, although 'b' is the weak 
reference, when I refer to 'b' it refers to the original object, which 
at this stage no longer exists.

So my revised question is -
   How can you remove the weak reference if you use proxy?

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?

However, I think that I have isolated the fundamental reason. So any 
comments on my latest findings will be appreciated.

Frank

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about weakref Frank Millman <frank@chagford.com> - 2012-07-06 09:00 +0200

csiph-web