Re: weak reference to bound method

2009-10-04 Thread ryles
On Oct 2, 4:54 am, Ole Streicher ole-usenet-s...@gmx.net wrote: Hi group, I am trying to use a weak reference to a bound method: class MyClass(object):     def myfunc(self):         pass o = MyClass() print o.myfunc   bound method MyClass.myfunc of __main__.MyClass object at 0xc675d0

weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi group, I am trying to use a weak reference to a bound method: class MyClass(object): def myfunc(self): pass o = MyClass() print o.myfunc bound method MyClass.myfunc of __main__.MyClass object at 0xc675d0 import weakref r = weakref.ref(o.myfunc) print r() None This is what

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
Ole Streicher wrote: I am trying to use a weak reference to a bound method: class MyClass(object): def myfunc(self): pass o = MyClass() print o.myfunc bound method MyClass.myfunc of __main__.MyClass object at 0xc675d0 import weakref r = weakref.ref(o.myfunc) print

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
= weakref.ref(func) def __call__(self): func = self._func() return func() if func else None 8-- This does not work for bound methods because the weak reference to a bound method will always point to None, even if the object still exists. Why

Re: weak reference to bound method

2009-10-02 Thread Miles Kaufmann
On Oct 2, 2009, at 1:54 AM, Ole Streicher wrote: I am trying to use a weak reference to a bound method: class MyClass(object): def myfunc(self): pass o = MyClass() print o.myfunc bound method MyClass.myfunc of __main__.MyClass object at 0xc675d0 import weakref r = weakref.ref

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hello Peter, Peter Otten __pete...@web.de writes: Is there an actual use case? I discussed this in the german newsgroup. Here is the use in my class: -8--- import threading import weakref class DoAsync(threading.Thread): def __init__(self,

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
__init__(self, func): self._func = weakref.ref(func) def __call__(self): func = self._func() return func() if func else None 8-- This does not work for bound methods because the weak reference to a bound method will always point

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Miles, Miles Kaufmann mile...@umich.edu writes: You could also create a wrapper object that holds a weak reference to the instance and creates a bound method on demand: class WeakMethod(object): def __init__(self, bound_method): self.im_func = bound_method.im_func

Re: weak reference to bound method

2009-10-02 Thread Thomas Lehmann
I am trying to use a weak reference to a bound method: class MyClass(object): def myfunc(self): pass o = MyClass() print o.myfunc bound method MyClass.myfunc of __main__.MyClass object at 0xc675d0 import weakref r = weakref.ref(o.myfunc) print r() None

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
Ole Streicher wrote: Peter Otten __pete...@web.de writes: class Method(object): def __init__(self, obj, func=None): if func is None: func = obj.im_func obj = obj.im_self This requires that func is a bound method. What I want is to have a universal

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hello Peter, Peter Otten __pete...@web.de writes: What I want is to have a universal class that always works: with unbound functions, with bound function, with lambda expressions, with locally defined functions, That's left as an exercise to the reader ;) Do you have the feeling that there

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
Ole Streicher wrote: Hello Peter, Peter Otten __pete...@web.de writes: What I want is to have a universal class that always works: with unbound functions, with bound function, with lambda expressions, with locally defined functions, That's left as an exercise to the reader ;) Do you

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Peter, Peter Otten __pete...@web.de writes: Ole Streicher wrote: Peter Otten __pete...@web.de writes: What I want is to have a universal class that always works: with unbound functions, with bound function, with lambda expressions, with locally defined functions, That's left as an

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Peter, Peter Otten __pete...@web.de writes: class Method(object): def __init__(self, obj, func=None): if func is None: func = obj.im_func obj = obj.im_self This requires that func is a bound method. What I want is to have a universal class that always

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
Ole Streicher wrote: Hi Peter, Peter Otten __pete...@web.de writes: Ole Streicher wrote: Peter Otten __pete...@web.de writes: What I want is to have a universal class that always works: with unbound functions, with bound function, with lambda expressions, with locally defined functions,

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Peter, Peter Otten __pete...@web.de writes: I am a bit surprised that already such a simple problem is virtually unsolvable in python. Btw, have you implemented such a design in another language? No. I think I'd go for a simpler approach, manage the lifetime of MyClass instances