| | |
How do I declare a class member function in another class?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 8
Reputation:
Solved Threads: 1
Hi,
I have a class
For each instance of SubDevice I want to define another action, so it's a parameter of SubDevice's class constructor.
Now some of the action functions need additional parameters, but that's normally not a problem because I can write something like But how do I pass the instance of
Thank you
Johannes Riecken
I have a class
SubDevice with a function action.For each instance of SubDevice I want to define another action, so it's a parameter of SubDevice's class constructor.
Now some of the action functions need additional parameters, but that's normally not a problem because I can write something like
Python Syntax (Toggle Plain Text)
sd1 = SubDevice(action=lambda: f1(param1, param2))
SubDevice as a parameter of the action function? I can't use self, because self refers to the class Device, where I instantiate SubDevice.Thank you
Johannes Riecken
Hi Johannes Riecken,
Could you please provide, class implementation structure, so that by looking at code we can help you.
BTW, i never used function as a class constructor argument. Sounds something interesting. I would surely want to look into. I would surely like to look at it.
kath.
•
•
•
•
But how do I pass the instance of SubDevice as a parameter of the action function? I can't use self, because self refers to the class Device, where I instantiate SubDevice.
BTW, i never used function as a class constructor argument. Sounds something interesting. I would surely want to look into. I would surely like to look at it.
kath.
Last edited by katharnakh; Sep 3rd, 2007 at 9:22 am.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
If I understand, you want something like this:
Now, I'm not sure where the variable parameter list would come into play. It seems to me that if you want a transparent interface, then you have to make the parameter list consistent. Can you give an example of what you had in mind?
Hope it helps,
Jeff
Python Syntax (Toggle Plain Text)
class SubDevice(object): def __init__(self, action): self.action = action def action1(): print "Hello!" def action2(): print "Goodbye!" sd1 = SubDevice(action1) sd2 = SubDevice(action2) sd1.action() sd2.action() >>> Hello! Goodbye!
Now, I'm not sure where the variable parameter list would come into play. It seems to me that if you want a transparent interface, then you have to make the parameter list consistent. Can you give an example of what you had in mind?
Hope it helps,
Jeff
•
•
Join Date: May 2006
Posts: 8
Reputation:
Solved Threads: 1
Hi,
As Sharma said one solution would be to construct subclasses of SubDevice for every different action and I thought about doing that in the beginning. However, my instructor is against it. Of course he's more procedurally oriented than younger programmers, but I agree that it would be too much overhead to construct subclasses, as they would be too specialized and they don't need any additional parameters.
Actually what jrcagle wrote is pretty much what I have in mind:
SuperDevice is larger device consisting of many different SubDevices. There could be numerous events when the plate_count has to decrease, so I have a separate method for it.
Now the problem is that I can't set the
Hope this helps!
Johannes
As Sharma said one solution would be to construct subclasses of SubDevice for every different action and I thought about doing that in the beginning. However, my instructor is against it. Of course he's more procedurally oriented than younger programmers, but I agree that it would be too much overhead to construct subclasses, as they would be too specialized and they don't need any additional parameters.
Actually what jrcagle wrote is pretty much what I have in mind:
Python Syntax (Toggle Plain Text)
class SubDevice(object): def __init__(self, action, super_device): self.action = action self.state = "ready" self.super_device = super_device def action1(): if super_device.plate_count > 0: super_device.plate_count -= 1 else: XXX.state = "error" def action2(): print "Goodbye!" class SuperDevice(object): def __init__(self, ...): self.plate_count = 100
Now the problem is that I can't set the
state field of SubDevice with action1, as I don't know what to replace XXX with.Hope this helps!
Johannes
Last edited by jonamasa; Sep 4th, 2007 at 4:04 am.
Hi,
I am not sure i understand your problem. I have some questions before i say something.
in the above code, is
If I believe it is,
you can
let me know how this works...
kath.
I am not sure i understand your problem. I have some questions before i say something.
Python Syntax (Toggle Plain Text)
def action1(): if super_device.plate_count > 0: super_device.plate_count -= 1 else: XXX.state = "error"
in the above code, is
super_device object of SuperDevice class and is global?If I believe it is,
•
•
•
•
Now the problem is that I can't set the state field of SubDevice with action1, as I don't know what to replace XXX with.
return "error" so that SubDevice which is calling that action function will get the state value.let me know how this works...
kath.
Last edited by katharnakh; Sep 4th, 2007 at 4:39 am.
•
•
Join Date: May 2007
Posts: 15
Reputation:
Solved Threads: 4
Is there any problem in making the self argument in the action functions mandatory?
python Syntax (Toggle Plain Text)
import functools class SubDevice(object): def __init__(self, action): #always pass a reference to self as first argument self.action = functools.partial(action, self) self.state = "ready" def action1(self): #the argument list has to contain self! if self.state == "ready": self.state = "error" else: self.state = "ready" a = SubDevice(action1) a.action() print a.state # 'error'
Last edited by ffao; Sep 11th, 2007 at 9:41 pm.
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Huh. There's something funky here that I don't understand. Because I don't have the module functools, I simplified a bit:
Shockingly, this throws an error:
Now, my understanding of the paradigm was that a.action() called the member method with a as the first parameter. Yet ... it doesn't!
When I replace with:
Then it works fine. So yes, I think there is an issue with what you want to do, and no, I don't (yet) know what it is.
Vega, thoughts?
Jeff
Python Syntax (Toggle Plain Text)
class SubDevice(object): def __init__(self, action): #always pass a reference to self as first argument self.action = action # functools.partial(action, self) self.state = "ready" def action1(self): #the argument list has to contain self! if self.state == "ready": self.state = "error" else: self.state = "ready" a = SubDevice(action1) a.action() #### <--- print a.state # 'error'
Shockingly, this throws an error:
Python Syntax (Toggle Plain Text)
Traceback (most recent call last): File "C:/Python24/subdevicetest.py", line 14, in -toplevel- a.action() TypeError: action1() takes exactly 1 argument (0 given)
Now, my understanding of the paradigm was that a.action() called the member method with a as the first parameter. Yet ... it doesn't!
When I replace with:
Python Syntax (Toggle Plain Text)
class SubDevice(object): def __init__(self, action): #always pass a reference to self as first argument self.action = action # functools.partial(action, self) self.state = "ready" def action1(self): #the argument list has to contain self! if self.state == "ready": self.state = "error" else: self.state = "ready" a = SubDevice(action1) a.action(a) ### <---- print a.state # 'error'
Then it works fine. So yes, I think there is an issue with what you want to do, and no, I don't (yet) know what it is.
Vega, thoughts?
Jeff
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Followup -- here's a quote from Yoda himself:
So Guido thinks your code oughta work.
Something funky is going on here. Or I'm not thinking straight. LOL
BTW, the clean way to do what you want is this:
Jeff
•
•
•
•
Originally Posted by Guido vR
What exactly happens when a method is called? You may have noticed that x.f() was called without an argument above, even though the function definition for f specified an argument. What happened to the argument? Surely Python raises an exception when a function that requires an argument is called without any--even if the argument isn't actually used ...
Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method's object before the first argument.
Something funky is going on here. Or I'm not thinking straight. LOLBTW, the clean way to do what you want is this:
Python Syntax (Toggle Plain Text)
class SubDevice(object): def __init__(self, action): #always pass a reference to self as first argument self.action = lambda : action(self) self.state = "ready" def action1(self): #the argument list has to contain self! if self.state == "ready": self.state = "error" else: self.state = "ready" a = SubDevice(action1) a.action() print a.state # 'error'
Jeff
Last edited by jrcagle; Sep 11th, 2007 at 10:24 pm. Reason: thought
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Ah. Many regards to Matthew Dixon Cowles over at Python-help, who says,
There it is. Following this lead, your code could become:
and that works.
I would not have predicted that... LOL
Jeff
•
•
•
•
Originally Posted by MDC
Dear Jeff,> Hi,Hello!
> I was trying to assist someone who wants to hook methods into
> objects post-instantiation.
> I'm really confused. I thought that a.action() automaticallyYou can stick a Python function pretty much anywhere:
> passed a as the first parameter.... print "wibble">>> def f():
...wibble>>> l=[f]
>>> l[0]()
But making a function an attribute of an object doesn't automatically
make it a method:... pass>>> class c:
...wibble>>> o=c()
>>> o.x=f
>>> o.x()
There, f is a plain function that's an attribute.
To make a function into an instance's method, you need the
instancemethod() function of the new module:... print "wibble">>> def m(self):
...wibble>>> import new
>>> o.m=new.instancemethod(m,o,c)
>>> o.m()
Regards,
Matt
Python Syntax (Toggle Plain Text)
import new class SubDevice(object): def __init__(self, action): #always pass a reference to self as first argument self.action = new.instancemethod(action,self,SubDevice) self.state = "ready" def action1(self): #the argument list has to contain self! if self.state == "ready": self.state = "error" else: self.state = "ready" a = SubDevice(action1) a.action() print a.state # 'error'
and that works.
I would not have predicted that... LOL
Jeff
![]() |
Similar Threads
- callbacks and class member functions... (C++)
- performance benefit by not calling static member function by object (C)
- Reading an input file as a class memeber function (C++)
- Abstract Class member function problem (C++)
- Visual Studio 6: Initializing static class member (C++)
- HELP: class static function - compile errors (C++)
Other Threads in the Python Forum
- Previous Thread: Python GUI Problem
- Next Thread: help de-serialize XML-RPC string
Views: 3424 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app assignment bash beginner bits bluetooth calling chmod cmd code convert data decimals dictionary dynamic dynamically examples excel external file float format ftp function gnu gui homework http import input itunes jaunty java keycontrol leftmouse line linux list lists loan loop maintain millimeter module mouse newb number numbers output parsing path pointer port prime program programming projects push py-mailer py2exe pygame pyqt python random recursion recursive scrolledtext slicenotation smtp split ssh string strings table tennis terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice windows wxpython xlib





