Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75657 > unrolled thread

how to call back a method in python3?

Started by"水静流深" <1248283536@qq.com>
First post2014-08-04 09:56 +0800
Last post2014-08-04 18:18 +1200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  how to call back  a method in python3? "水静流深" <1248283536@qq.com> - 2014-08-04 09:56 +0800
    Re: how to call back  a method in python3? Marko Rauhamaa <marko@pacujo.net> - 2014-08-04 08:47 +0300
      Re: how to call back  a method in python3? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-08-04 18:18 +1200

#75657 — how to call back a method in python3?

From"水静流深" <1248283536@qq.com>
Date2014-08-04 09:56 +0800
Subjecthow to call back a method in python3?
Message-ID<mailman.12616.1407121329.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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:


      File "<stdin>", line 1
        def callback(self.do,x):
                     ^
    SyntaxError: invalid syntax


`do` is a method in my class ,how to write the code?‍

[toc] | [next] | [standalone]


#75662

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-08-04 08:47 +0300
Message-ID<87y4v4kdqk.fsf@elektro.pacujo.net>
In reply to#75657
"水静流深" <1248283536@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

[toc] | [prev] | [next] | [standalone]


#75663

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-08-04 18:18 +1200
Message-ID<c48mtrFim9vU1@mid.individual.net>
In reply to#75662
Marko Rauhamaa wrote:
> 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))

Or more simply:

       def my_method(self):
          return self.do

-- 
Greg

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web