On Aug 20, 11:19 pm, Ricardo Aráoz <[EMAIL PROTECTED]> wrote: > Terry Reedy wrote: > > > [EMAIL PROTECTED] wrote: > >> Is the following code is ok. who to call all method. > >> It is working but the call to m() without a reference to self seems > >> strange > > > The reference to self is bound to the methods by the way you look them up. > > >> class CustomMethod: > >> def method1(self): > >> .... > >> def method2(self): > >> .... > >> def method3(self): > >> .... > > >> def getAllMethod(self): > >> return [self.method1, self.method2, self.method3] > > >> def applyAll(self): > >> for m in self.getAllMethod(): > >> m() > > > If the list is static, there is no need to calculate it more than once, > > at class-definition time. I might do this like so: > > > class CustomMethod: > > ... > > all_methods = [method1, method2, method3] > > def apply_all(self): > > for m in self.all_methods: > > m(self) > > > Class code has access to the results of previous class code. > > BTW, how would you guys go about registering the functions to be > returned by apply_all()? > I mean something like : > > class CustomMethod(object) : > def __init__(self) : > self.methodList = [] > > def method1(self) : > ... > self.registerMe(????send a reference to this method????) > > def method2(self) : > ... > self.registerMe(????send a reference to this method????) > > def registerMe(????receive a reference to some method????) : > self.methodList.append(????the reference to the method????) > > def getAllMethods(self) : > return self.methodList > > etc.
It's a bit hard to see how a method could register itself on a list of methods to be called, without being called ... I'd put all_methods = [ method1, method2, etc ] at the end of the class. BTW, I don't get the use case for calling all methods in a list; when/ why would you want to do that? In the case where you want to process some data and call methods dependent on the data (e.g. XML element tag), you can use something like this: tag2method = { 'custname': handle_name, 'custaddr': handle_address, etc } HTH, John -- http://mail.python.org/mailman/listinfo/python-list