Op 2005-11-27, Flavio schreef <[EMAIL PROTECTED]>: > hi, > > I have an object defined with a number of hardcoded methods. > > Class soandso: > def __init__(self): > self.this = 0 > self.that = 1 > def meth1(self): > ... > def meth2(self): > ... > def custom(self): > pass > > I want to allow the user to write a python module that declares a > function so that myprogram can import it and attribute it to the custom > method of the soandso object. So far so good, that is an easy thing to > do in Python. > > import usermodule > a=soandso() > a.custom = usermodule.function > > But, what if the method had to access the self attributes (self.this > and self.that) of the soandso object? > > Can it be done? and if so, what is the most Pythonic way of doing it?
You mean something like this: >>> class Foo: ... def __init__(self, v): ... self.value = v ... >>> def show(self): ... print self.value ... >>> import new >>> f = Foo(17) >>> f.show = new.instancemethod(show, f) >>> f.show() 17 -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list