469,602 Members | 1,645 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

who to call a list of method inside the class itself

Hi,

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

Thanks for your help

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():
# how to call all methods ?
# is it correct
m()

Aug 19 '08 #1
5 1327
ma****@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

Thanks for your help

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():
# how to call all methods ?
# is it correct
m()
what happens when you run the code?

</F>

Aug 19 '08 #2
On Aug 19, 4:33 pm, Fredrik Lundh <fred...@pythonware.comwrote:
mad...@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
Thanks for your help
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():
# how to call all methods ?
# is it correct
m()

what happens when you run the code?

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

-Stephane
Aug 19 '08 #3
ma****@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>

Aug 19 '08 #4
En Tue, 19 Aug 2008 11:18:00 -0300, <ma****@pt.luescribi�:
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
Ok, so you already know it works - and you want to know why!
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():
# how to call all methods ?
# is it correct
m()
When you write self.method1, you get a "bound method" - it already knows
the instance on which it is called (self, in this case).
When you write CustomMethod.method1, you get an "unbound method" - it is
not bound to any particular instance; you must provide the instance
yourself when calling it.
See "User-defined methods" in http://docs.python.org/ref/types.html - if
you really want to know how this works, read about new-style classes and
the descriptor protocol at http://www.python.org/doc/newstyle/

--
Gabriel Genellina

Aug 20 '08 #5
On Aug 20, 8:14 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Tue, 19 Aug 2008 11:18:00 -0300, <mad...@pt.luescribi :
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

Ok, so you already know it works - and you want to know why!
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():
# how to call all methods ?
# is it correct
m()

When you write self.method1, you get a "bound method" - it already knows
the instance on which it is called (self, in this case).
When you write CustomMethod.method1, you get an "unbound method" - it is
not bound to any particular instance; you must provide the instance
yourself when calling it.
See "User-defined methods" inhttp://docs.python.org/ref/types.html- if
you really want to know how this works, read about new-style classes and
the descriptor protocol athttp://www.python.org/doc/newstyle/

--
Gabriel Genellina
Tanks all for your help

Regards

-Stephane
Aug 20 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Daniel Kay | last post: by
3 posts views Thread by Lakesider | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.