469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Re: who to call a list of method inside the class itself


ma****@pt.lu wrote:
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()
1. return string names of required methods in getAllMethod
return ['method1', 'method2', 'method3']
2. use gettattr on self and then exetute methods in applyAll
def applyAll(self):
for method_name in self.getAllMethod():
method = gettattr(self,method_name)
method() #execute method now

regards.
Edwin


The information contained in this message and any attachment may be
proprietary, confidential, and privileged or subject to the work
product doctrine and thus protected from disclosure. If the reader
of this message is not the intended recipient, or an employee or
agent responsible for delivering this message to the intended
recipient, you are hereby notified that any dissemination,
distribution or copying of this communication is strictly prohibited.
If you have received this communication in error, please notify me
immediately by replying to this message and deleting it and all
copies and backups thereof. Thank you.
Aug 19 '08 #1
3 1260

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
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.

tjr

Aug 19 '08 #2
Terry Reedy wrote:
>
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

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.

Aug 20 '08 #3
On Aug 20, 11:19 pm, Ricardo ArŠoz <ricar...@gmail.comwrote:
Terry Reedy wrote:
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
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
Aug 21 '08 #4

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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.