who to call a list of method inside the class itself

Fredrik Lundh fredrik at pythonware.com
Tue Aug 19 11:04:18 EDT 2008


maduma at pt.lu wrote:

>>>     def getAllMethod(self):
>>>         return [self.method1, self.method2, self.method3]
>>>     def applyAll(self):
>>>         for m in self.getAllMethod():
>>>             # how to call all methods ?
>>>             # is it correct
>>>             m()
>> what happens when you run the code?

> The code it is running fine but i just wondering if it's the syntax is
> correct (avoid any side effect)

It's the same thing as explicitly calling the three methods from inside 
the applyAll method.  You'll still get side effects if the methods have 
side effects, of course.

self.method1 and friends are "bound methods", that is, callable objects 
that are bound to both the object instance (self) and the actual method. 
    They're no different from the temporary bound methods that are used 
to carry out an ordinary method call ("self.method1()" is evaluated as 
"tmp = self.method1; tmp()" on the inside, where tmp is an internal 
variable)

</F>




More information about the Python-list mailing list