Calling member functions? 
Author Message
 Calling member functions?

Hi,

I can't quite get this right. How can I give an object instance and a method
as a callback for some other module/class to call at a later point?

class  Handler:
    def Callback(self):
        print "Callback called"

class Notifier:

    def __init__(self, object, method):
        self.m_object = object
        self.m_method = method

    def notify(self):
        ## Now what??
        self.m_method(self.m_object) ## naive attempt

h = Handler()
n = Notifier( h, h.Callback )
h.notify()  # I want this to do h.Callback() in effect

Thanks
David Dawkins
--
david_j_dawkins  x/AT/x  hotmail.com



Mon, 31 May 2004 19:20:52 GMT  
 Calling member functions?

Quote:

> class  Handler:
>     def Callback(self):
>         print "Callback called"

> class Notifier:

>     def __init__(self, object, method):
>         self.m_object = object
>         self.m_method = method

>     def notify(self):
>         ## Now what??
>         self.m_method(self.m_object) ## naive attempt

               ^^^^^^^^ You call a method with one argument, but the
Callback method does not accept any arguments.

Quote:
> h = Handler()
> n = Notifier( h, h.Callback )
> h.notify()  # I want this to do h.Callback() in effect

   h.notify()? You certainly meant n.notify()! Other than that you naive
attempt is pretty close!

Oleg.
--

           Programmers don't die, they just GOSUB without RETURN.



Mon, 31 May 2004 19:34:46 GMT  
 Calling member functions?


Quote:

> > class  Handler:
> >     def Callback(self):
> >         print "Callback called"

> > class Notifier:

> >     def __init__(self, object, method):
> >         self.m_object = object
> >         self.m_method = method

> >     def notify(self):
> >         ## Now what??
> >         self.m_method(self.m_object) ## naive attempt

>                ^^^^^^^^ You call a method with one argument, but the
> Callback method does not accept any arguments.

but then how does Handler.Callback receive its own "self" value? In this
call, "self" is an instance of Notifier. Handler.Callback must receive an
instance of Handler, which is stored in self.m_object.

Quote:

> > h = Handler()
> > n = Notifier( h, h.Callback )
> > h.notify()  # I want this to do h.Callback() in effect

>    h.notify()? You certainly meant n.notify()! Other than that you naive
> attempt is pretty close!

You're right, I made a typo as I created the example for this posting.

- Show quoted text -

Quote:

> Oleg.
> --

>            Programmers don't die, they just GOSUB without RETURN.



Mon, 31 May 2004 20:22:17 GMT  
 Calling member functions?


Quote:
> I can't quite get this right. How can I give an object instance and a
> method as a callback for some other module/class to call at a later
> point?

Generally you dont. You just pass the method on its own:

class  Handler:
    def Callback(self):
        print "Callback called"

class Notifier:
    def __init__(self, method):
        self.m_method = method

    def notify(self):
        self.m_method()

h = Handler()
n = Notifier(h.Callback)
n.notify()  # Calls h.Callback()

h.Callback is a bound method. If you call it, then it calls the Callback
method on the h instance. What you were doing was attempting to use it as
though it were an unbound method. Calls to h.Callback take no arguments.

Handler.Callback would be the unbound method. Calls to Handler.Callback
take one argument. Your original code probably works if you pass in h and
Handler.Callback, but usually it is cleaner to pass a single object for the
callback rather than two.

A third option is to pass in h and the name of the callback method as a
string and call it using getattr, but while this is sometimes useful it is
generally messy.

--

int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



Mon, 31 May 2004 20:51:16 GMT  
 Calling member functions?

Quote:

> > >         self.m_method(self.m_object) ## naive attempt

> >                ^^^^^^^^ You call a method with one argument, but the
> > Callback method does not accept any arguments.

> but then how does Handler.Callback receive its own "self" value? In this

   m_method is not a method - it is *bound* method! It is h.Callback - an
object, that stores both h and Callback! So when you call it, python gets
the self for Callback from the bound method; the self will by just h!
Understand?

                       vvvvvvvvvv - you initialized m_method with the bound method

Quote:
> > > n = Notifier( h, h.Callback )

Oleg.
--

           Programmers don't die, they just GOSUB without RETURN.


Mon, 31 May 2004 20:35:09 GMT  
 Calling member functions?

Quote:

> class  Handler:
>     def Callback(self):
>         print "Callback called"
> class Notifier:
>     def __init__(self, object, method):
>         self.m_object = object
>         self.m_method = method
>     def notify(self):
>         ## Now what??
>         self.m_method(self.m_object) ## naive attempt
> h = Handler()
> n = Notifier( h, h.Callback )
> h.notify()  # I want this to do h.Callback() in effect

(i'll assume you mean n.notify in the last line.)

when you instantiate n with "h.Callback", you're assigning a *bound*
function reference to n.m_method.  you can see this by just printing
out the value of n.m_method.  you'll get something like:

<method Handler.Callback of Handler instance at 0x80dba14>

thus, when you call m_method(), you don't have to provide an instance
for the "self" variable because the function is already bound to an
instance (specifically, to h).

if you change your code a little bit to the following:

class Notifier:
        def __init__(self, object, method):
                self.m_object = object
                self.m_method = method
        def notify(self):
                self.m_method(self.m_object) ## naive attempt
h = Handler()
n = Notifier(h, Handler.Callback)
n.notify()

this will also work.  notice that here, n is instantiated with an
*unbound* function, named Handler.Callback (which is different from
h.Callback).  in this case, if you want to call n.m_method(), you have
to provide an instance for the "self" parameter, so you have to pass
in self.m_object.

incidentally, if you print out the value of n.m_method in this
modified code, you get:

<unbound method Handler.Callback>

HTH,
peter



Tue, 01 Jun 2004 05:44:10 GMT  
 Calling member functions?


Quote:

> > > >         self.m_method(self.m_object) ## naive attempt

> > >                ^^^^^^^^ You call a method with one argument, but the
> > > Callback method does not accept any arguments.

> > but then how does Handler.Callback receive its own "self" value? In this

>    m_method is not a method - it is *bound* method! It is h.Callback - an
> object, that stores both h and Callback! So when you call it, Python gets
> the self for Callback from the bound method; the self will by just h!
> Understand?

>                        vvvvvvvvvv - you initialized m_method with the
bound method
> > > > n = Notifier( h, h.Callback )

Yes, I understand now! Thanks for your help.

David



Tue, 01 Jun 2004 02:39:15 GMT  
 Calling member functions?


Quote:


> > I can't quite get this right. How can I give an object instance and a
> > method as a callback for some other module/class to call at a later
> > point?

> Generally you dont. You just pass the method on its own:

> class  Handler:
>     def Callback(self):
>         print "Callback called"

> class Notifier:
>     def __init__(self, method):
>         self.m_method = method

>     def notify(self):
>         self.m_method()

> h = Handler()
> n = Notifier(h.Callback)
> n.notify()  # Calls h.Callback()

> h.Callback is a bound method. If you call it, then it calls the Callback
> method on the h instance. What you were doing was attempting to use it as
> though it were an unbound method. Calls to h.Callback take no arguments.

> Handler.Callback would be the unbound method. Calls to Handler.Callback
> take one argument. Your original code probably works if you pass in h and
> Handler.Callback, but usually it is cleaner to pass a single object for
the
> callback rather than two.

> A third option is to pass in h and the name of the callback method as a
> string and call it using getattr, but while this is sometimes useful it is
> generally messy.

OK, I see. It all makes sense now. It's like I curried the function with
"h.Callback".

Thanks for your help
David



Tue, 01 Jun 2004 02:40:29 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Calling class member functions from C++

2. Calling C++ object member functions from ass ?K

3. Indirect function member calls

4. VC++ calling fortran function and fortran function calling a c++ function

5. How to find out name of calling function from called function

6. Calling functions from functions from functions ...

7. function definition in member module

8. How Non-Member Functions Improve Encapsulation

9. How Non-Member Functions Improve Encapsulation

10. How to write class member functions in ASM ?

11. ASM implementation of member function

12. static member function

 

 
Powered by phpBB® Forum Software