473,554 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(se lf):
return [self.method1, self.method2, self.method3]

def applyAll(self):
for m in self.getAllMeth od():
# how to call all methods ?
# is it correct
m()

Aug 19 '08 #1
5 1453
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(se lf):
return [self.method1, self.method2, self.method3]

def applyAll(self):
for m in self.getAllMeth od():
# 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...@python ware.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(se lf):
return [self.method1, self.method2, self.method3]
def applyAll(self):
for m in self.getAllMeth od():
# 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(se lf):
return [self.method1, self.method2, self.method3]
def applyAll(self):
for m in self.getAllMeth od():
# 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.lues cribi�:
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(se lf):
return [self.method1, self.method2, self.method3]

def applyAll(self):
for m in self.getAllMeth od():
# 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.me thod1, 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.a r>
wrote:
En Tue, 19 Aug 2008 11:18:00 -0300, <mad...@pt.lues cribi :
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(se lf):
return [self.method1, self.method2, self.method3]
def applyAll(self):
for m in self.getAllMeth od():
# 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.me thod1, 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
2510
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
9
3657
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 { /* ... */ }; template<class T> classSubject {
7
8227
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
1272
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 refresh in A. My question: HOW can I call A.refresh() from B??? What is here a "best practice"?
10
2299
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 call to work? Exists is a method within the Swatch class NOT the SwatchPanel class. Is this possible? Suggestions would be very welcome.
9
5833
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context,...
4
1898
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 SetName("none") or just set name="none"?
6
14865
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
3
1427
by: Edwin.Madari | last post by:
maduma@pt.lu wrote: 1. return string names of required methods in getAllMethod return 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
0
7496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7778
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7857
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5412
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5135
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1992
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1109
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.