Re: Storing a callback function as a class member

2010-07-08 Thread Nathan Huesken
Hey,

Sorry, I tried to sent only the relevant parts of the example, but the
part where the error was, was left out.

I defined the function, used as callback like this:
class SomeClass:
def callback(param):
...

So I forgot the self parameter, and therefor the callback had a
different number of parameters than I expected.

Thanks for the effort!
Nathan

-- 
http://mail.python.org/mailman/listinfo/python-list


Storing a callback function as a class member

2010-07-07 Thread Nathan Huesken
Hi,

I have a class, where I want to store a callback function as a member
to access later:

class CallbackClass:
def setCallback(self,cb):
self.cb = cb

def callCallback(self, para):
self.cb(para)

Doing so, I get the error:
callbackFunc() takes exactly 1 parameter (2 given)

self is given as parameter this way, is it not? How can this be done?

Thanks!
Nathan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a callback function as a class member

2010-07-07 Thread MRAB

Nathan Huesken wrote:

Hi,

I have a class, where I want to store a callback function as a member
to access later:

class CallbackClass:
def setCallback(self,cb):
self.cb = cb

def callCallback(self, para):
self.cb(para)

Doing so, I get the error:
callbackFunc() takes exactly 1 parameter (2 given)

self is given as parameter this way, is it not? How can this be done?


Could you provide a short program which we could run to reproduce the
problem?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a callback function as a class member

2010-07-07 Thread Rhodri James
On Wed, 07 Jul 2010 22:48:11 +0100, Nathan Huesken  
pyt...@lonely-star.org wrote:



Hi,

I have a class, where I want to store a callback function as a member
to access later:

class CallbackClass:
def setCallback(self,cb):
self.cb = cb

def callCallback(self, para):
self.cb(para)

Doing so, I get the error:
callbackFunc() takes exactly 1 parameter (2 given)

self is given as parameter this way, is it not? How can this be done?


rho...@gnudebst:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type help, copyright, credits or license for more information.

class CBClass:

... def set_cb(self, cb):
... self.cb = cb
... def call_cb(self, para):
... self.cb(para)
...

def trivial(arg):

... print arg
...

c = CBClass()
c.set_cb(trivial)
c.call_cb(Hello, world)

Hello, world

Works for me.  Which version of Python are you using?

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a callback function as a class member

2010-07-07 Thread Emile van Sebille

On 7/7/2010 2:48 PM Nathan Huesken said...

class CallbackClass:
 def setCallback(self,cb):
 self.cb = cb

 def callCallback(self, para):
 self.cb(para)




You'll have to show how you're invoking this -- the following works for 
me (ie, I don't get an error):


class CallbackClass:
def setCallback(self,cb):
self.cb = cb
def callCallback(self, para):
self.cb(para)


a = CallbackClass()


def test(param): return 2*param


a.setCallback(test)

a.callCallback(3)



Emile

--
http://mail.python.org/mailman/listinfo/python-list


Re: Storing a callback function as a class member

2010-07-07 Thread Ian Kelly
On Wed, Jul 7, 2010 at 3:48 PM, Nathan Huesken pyt...@lonely-star.org wrote:
 Hi,

 I have a class, where I want to store a callback function as a member
 to access later:

 class CallbackClass:
    def setCallback(self,cb):
        self.cb = cb

    def callCallback(self, para):
        self.cb(para)

 Doing so, I get the error:
 callbackFunc() takes exactly 1 parameter (2 given)

 self is given as parameter this way, is it not? How can this be done?

No, self will not be passed as a parameter.  A function is only
treated as a method when it is present in the class dict.  If it is in
the instance dict as you have above, then it's just a normal function.
 If you want it to receive self in this case, then you should have
your callCallback method pass it in explicitly.

HTH,
Ian
-- 
http://mail.python.org/mailman/listinfo/python-list