who to call a list of method inside the class itself
Ricardo Aráoz
ricaraoz at gmail.com
Wed Aug 20 09:19:02 EDT 2008
Terry Reedy wrote:
>
> maduma at pt.lu 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.
More information about the Python-list
mailing list