Re: weak reference to bound method

2009-10-04 Thread ryles
On Oct 2, 4:54 am, Ole Streicher 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 > >   > > > import weakref > r = weakref.ref(o.myfunc) > print r() > >   Non

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 > i

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 >

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 "

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, >>> T

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 re

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 t

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

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 > > > > import weakref > r = weakref.ref(o.myfunc) > print r() > None > > This is what I do not understand. The object "o" is s

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Miles, Miles Kaufmann 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 > self.im_self =

Re: weak reference to bound method

2009-10-02 Thread Peter Otten
Ole Streicher wrote: > Hi Thomas, > > Thomas Lehmann writes: >>> r = weakref.ref(o.myfunc) >>> print r() >>> None >> k = o.myfunc >> r = weakref.ref(k) >> print r() >>> > >> Don't ask me why! I have just been interested for what you are trying... > > This is clear: in your case, o

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 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 0xc675d0>> import weakref r = weakref.ref(o.myfunc) print r() None This is what I do not understand

Re: weak reference to bound method

2009-10-02 Thread Ole Streicher
Hi Thomas, Thomas Lehmann writes: >> r = weakref.ref(o.myfunc) >> print r() >> None > k = o.myfunc > r = weakref.ref(k) > print r() >> > Don't ask me why! I have just been interested for what you are trying... This is clear: in your case, o.myfunc is explicitely referenced by k, th

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 > 0xc675d0>> > > import weakref > r = weakref.ref(o.myfunc) > print r() > None > > This is what