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
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
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
= 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
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
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,
__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
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
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
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
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
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
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
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
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,
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
16 matches
Mail list logo