Re: weakrefs and bound methods

2007-10-11 Thread Mathias Panzenboeck
Bruno Desthuilliers wrote: Mathias Panzenboeck a écrit : About the lost weakref problem: in Python, methods are just tiny wrappers around the object, class and function created at lookup time (yes, on *each* lookup) (and WWAI, they are by the function object itself, which implements the

Re: weakrefs and bound methods

2007-10-08 Thread Steven D'Aprano
On Mon, 08 Oct 2007 04:06:55 +, Michele Simionato wrote: Hmmm... I'm not sure I understand how a with statement is meant to replace class.__del__ in practice. It seems to me that the two things have different uses. with is useful when you create an object, do something with it,

Re: weakrefs and bound methods

2007-10-08 Thread Chris Mellon
On 10/8/07, Steven D'Aprano [EMAIL PROTECTED] wrote: On Mon, 08 Oct 2007 04:06:55 +, Michele Simionato wrote: Hmmm... I'm not sure I understand how a with statement is meant to replace class.__del__ in practice. It seems to me that the two things have different uses. with is useful

Re: weakrefs and bound methods

2007-10-08 Thread Bruno Desthuilliers
Mathias Panzenboeck a écrit : About the lost weakref problem: in Python, methods are just tiny wrappers around the object, class and function created at lookup time (yes, on *each* lookup) (and WWAI, they are by the function object itself, which implements the descriptor protocol). When I

weakrefs and bound methods

2007-10-07 Thread Mathias Panzenboeck
Hi. I have a problem with weak refs and bound methods. The best explanation for the problem is a short bit of code. I have the three classes Wrapper, Foo and Bar: import weakref class Wrapper(object): def __init__(self,x): self.x = weakref.ref(x) def

Re: weakrefs and bound methods

2007-10-07 Thread Mathias Panzenboeck
When I change the class Wrapper to following, the class Foo works: class Wrapper(object): def __init__(self,x): self.func_name = x.func_name self.x = weakref.ref(x.im_self) def __call__(self,*args,**kwargs): x = self.x()

Re: weakrefs and bound methods

2007-10-07 Thread Marc 'BlackJack' Rintsch
On Sun, 07 Oct 2007 16:51:33 +0200, Mathias Panzenboeck wrote: import weakref class Wrapper(object): def __init__(self,x): self.x = weakref.ref(x) def __call__(self,*args,**kwargs): x = self.x() if x is None:

Re: weakrefs and bound methods

2007-10-07 Thread Michele Simionato
On Oct 7, 12:26 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: Drop all those `__del__()` methods as they prevent the garbage collector from collecting cycles. I fully agree and I will add that __del__ methods are always a bad idea. Also notice that recently Raymond Hetting said in this

Re: weakrefs and bound methods

2007-10-07 Thread Mathias Panzenboeck
Marc 'BlackJack' Rintsch wrote: On Sun, 07 Oct 2007 16:51:33 +0200, Mathias Panzenboeck wrote: import weakref class Wrapper(object): def __init__(self,x): self.x = weakref.ref(x) def __call__(self,*args,**kwargs): x = self.x() if x is

Re: weakrefs and bound methods

2007-10-07 Thread Mathias Panzenboeck
Marc 'BlackJack' Rintsch wrote: ``del b`` just deletes the name `b`. It does not delete the object. There's still the name `_` bound to it in the interactive interpreter. `_` stays bound to the last non-`None` result in the interpreter. Actually I have the opposite problem. The reference

Re: weakrefs and bound methods

2007-10-07 Thread Steven D'Aprano
On Sun, 07 Oct 2007 16:38:23 +, Michele Simionato wrote: On Oct 7, 12:26 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: Drop all those `__del__()` methods as they prevent the garbage collector from collecting cycles. I fully agree and I will add that __del__ methods are always

Re: weakrefs and bound methods

2007-10-07 Thread Steven Bethard
Mathias Panzenboeck wrote: Marc 'BlackJack' Rintsch wrote: ``del b`` just deletes the name `b`. It does not delete the object. There's still the name `_` bound to it in the interactive interpreter. `_` stays bound to the last non-`None` result in the interpreter. Actually I have the

Re: weakrefs and bound methods

2007-10-07 Thread Michele Simionato
On Oct 7, 1:14 pm, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: On Sun, 07 Oct 2007 16:38:23 +, Michele Simionato wrote: On Oct 7, 12:26 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: Drop all those `__del__()` methods as they prevent the garbage collector from

Re: weakrefs and bound methods

2007-10-07 Thread Alex Martelli
Steven D'Aprano [EMAIL PROTECTED] wrote: ... Without __del__, what should I have done to test that my code was deleting objects and not leaking memory? See module gc in the Python standard library. What should I do when my objects need to perform some special processing when they are

Re: weakrefs and bound methods

2007-10-07 Thread Alex Martelli
Mathias Panzenboeck [EMAIL PROTECTED] wrote: ... I only inserted them so I can see if the objects are really freed. How can I see that without a __del__ method? You can use weakref.ref instances with finalizer functions - see the long post I just made on this thread for a reasonably rich and

Re: weakrefs and bound methods

2007-10-07 Thread Alex Martelli
. Actually I have the opposite problem. The reference (to the bound method) gets lost but it shouldn't! weakrefs to bound methods require some subtlety, see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81253 (or what I believe is the better treatment of this recipe in the printed edition

Re: weakrefs and bound methods

2007-10-07 Thread Mathias Panzenboeck
in the interpreter. Actually I have the opposite problem. The reference (to the bound method) gets lost but it shouldn't! weakrefs to bound methods require some subtlety, see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81253 (or what I believe is the better treatment of this recipe

Re: weakrefs and bound methods

2007-10-07 Thread Steven D'Aprano
On Sun, 07 Oct 2007 12:55:29 -0700, Alex Martelli wrote: What should I do when my objects need to perform some special processing when they are freed, if I shouldn't use __del__? The solid, reliable way is: from __future__ import with_statement and use module contextlib from the

Re: weakrefs and bound methods

2007-10-07 Thread Michele Simionato
On Oct 7, 11:28 pm, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sun, 07 Oct 2007 12:55:29 -0700, Alex Martelli wrote: What should I do when my objects need to perform some special processing when they are freed, if I shouldn't use __del__? The solid, reliable way is: from __future__