473,396 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1418

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
7
by: Action | last post by:
in c++ if i put static int count = 0; in a method; count can only accessed inside the method and retain it's value. is there equilvant thing in C#? thx
3
by: Lakesider | last post by:
Hi NG, my problem is: I have a class A with a map on it. There is a public methode "Refresh" for this map. Within A i call a class B (new B). In B I modify some things, that would need to do a...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: Travis | last post by:
Is it considered good practice to call a mutator when inside the same class or modify the attribute directly? So if there's a public method SetName() would it be better from say ::Init() to call...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
5
by: maduma | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.