how to call back a method in python3?

Marko Rauhamaa marko at pacujo.net
Mon Aug 4 01:47:31 EDT 2014


"水静流深" <1248283536 at qq.com>:

> I want to call back a function which is the method of a class .
>
>     def callback(self.do,x):
>         return(self.do(x))
>
> That is what i want to write,when i input 
>      
>     def callback(self.do,x):
>
> error message:

Do this:

    class MyClass:
        def my_method(self):
            def callback(x):
                return self.do(x)
            return callback

        def do(self, x):
            print("done: {}".format(x))

then call:

    m = MyClass()
    callback = m.my_method()
    callback(7)


Marko



More information about the Python-list mailing list