By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,347 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

How to bound a method to a instance ?

P: n/a
Hi,
I would like to know how to bound a method to a instance of a class.

One application would be to allow a change in a method after a reload :

module toto :
--------------
class toto:
def method(self):
(...)
main module :
--------------
import toto

a = toto.toto()
a.method() -> does something ...

reload (toto)
now a is not reloaded...

a.method = toto.toto().method
does not work, because a.method would be bounded to a new instance...
Thanks,

Emmanuel...

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Emmanuel wrote:
main module :
--------------
import toto

a = toto.toto()
a.method() -> does something ...

reload (toto)
now a is not reloaded...

a.method = toto.toto().method
does not work, because a.method would be bounded to a new instance...


It might be best to change the class of a:

reload (toto)
a.__class__ = toto.toto

Then, a.method will automatically become the new method. Of course,
this will also change all other methods of toto, not just method.

If you really want to change only method, you could do

a.__class__.__dict__['method'] = toto.toto.__dict__['method']

That would change just method, but of all instances of the old
definition of toto.toto.

If you really want to change only one method, and only of one
object, you need to use new.instancemethod:

a.method = new.instancemethod(toto.toto.method.im_func, a, a.__class__)

Regards,
Martin

Jul 18 '05 #2

P: n/a
Wonderfull !!

I thank you very much for this clear explanation...

Emmanuel

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?= a crit :
Emmanuel wrote:
main module :
--------------
import toto

a = toto.toto()
a.method() -> does something ...

reload (toto)
now a is not reloaded...

a.method = toto.toto().method
does not work, because a.method would be bounded to a new instance...


It might be best to change the class of a:

reload (toto)
a.__class__ = toto.toto

Then, a.method will automatically become the new method. Of course,
this will also change all other methods of toto, not just method.

If you really want to change only method, you could do

a.__class__.__dict__['method'] = toto.toto.__dict__['method']

That would change just method, but of all instances of the old
definition of toto.toto.

If you really want to change only one method, and only of one
object, you need to use new.instancemethod:

a.method = new.instancemethod(toto.toto.method.im_func, a, a.__class__)

Regards,
Martin


Jul 18 '05 #3

P: n/a
"Martin v. Lwis" wrote:
Emmanuel wrote:
main module :
--------------
import toto

a = toto.toto()
a.method() -> does something ...

reload (toto)
now a is not reloaded...

a.method = toto.toto().method
does not work, because a.method would be bounded to a new instance...


It might be best to change the class of a:

reload (toto)
a.__class__ = toto.toto


Thank you emmanuel for asking this question, I was wondering exactly the
same thing. Is it also possible to override the reload method to make it
(besides reloading) to scan through all namespaces for objects of class
toto.toto and replace their methods automatically? What would that code
look like?

Thanks,

Maarten

--
================================================== =================
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #4

P: n/a
Maarten van Reeuwijk wrote:
Thank you emmanuel for asking this question, I was wondering exactly the
same thing. Is it also possible to override the reload method to make it
(besides reloading) to scan through all namespaces for objects of class
toto.toto and replace their methods automatically? What would that code
look like?


In general, it is impossible or atleast very inefficient to do that.
It may be easiest if toto.toto would prepare itself for reloading:

import weakref
try:
weakref.totos
except AttributeError:
weakref.totos = []

class toto:
def __init__(self, args):
weakref.totos.append(weakref.ref(self))
...

def update_totos():
for t in weakref.totos:
t = t()
if t: t.__class__ = toto

def shrink_totos():
totos = []
for t in weakref.totos:
if t(): totos.append(t)
weakref.totos=[]

Then, after reloading toto, invoke toto.update_totos(). From
time to time, invoke weakref.shrink_totos(), to discard unused
weak references.

If you absolutely want to update all objects without preparing
a list earlier, you can use gc.get_objects() to find all containers;
you need to find out which of those are totos. Don't compare the
classes of the objects for identity (o.__class__ is toto.toto), unless
you have a reference of the old class (i.e. before reloading).

Regards,
Martin

Jul 18 '05 #5

P: n/a
> def shrink_totos():
totos = []
for t in weakref.totos:
if t(): totos.append(t)
weakref.totos=[]

^^ should be totos

- Josiah
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.