How to write a simple callback function?

前端 未结 6 1929
我在风中等你
我在风中等你 2021-02-04 06:11

Python 2.7.10

I wrote the following code to test a simple callback function.

def callback(a, b):
    print(\'Sum = {0}\'.format(a+b))

def main(         


        
相关标签:
6条回答
  • 2021-02-04 06:16

    Your code is executed as follows:

    main(callback(1, 2))
    

    callback function is called with (1, 2) and it returns None (Without return statement, your function prints Sum = 3 and returns None)

    main function is called with None as argument (So callback != None will always be False)

    0 讨论(0)
  • 2021-02-04 06:23

    As mentioned in the comments, your callback is called whenever it's suffixed with open and close parens; thus it's called when you pass it.

    You might want to use a lambda and pass in the values.

    #!/usr/bin/env python3
    
    def main(callback=None, x=None, y=None):
        print('Add any two digits.')
        if callback != None and x != None and y != None:
            print("Result of callback is {0}".format(callback(x,y)))
        else:
            print("Missing values...")
    
    if __name__ == "__main__":
        main(lambda x, y: x+y, 1, 2)
    
    0 讨论(0)
  • 2021-02-04 06:29

    Here's what you wanted to do :

    def callback(a, b):
        print('Sum = {0}'.format(a+b))
    
    def main(a,b,f=None):
        print('Add any two digits.')
        if f != None:
            f(a,b)
    
    main(1, 2, callback)
    
    0 讨论(0)
  • 2021-02-04 06:30

    This is an old post, but perhaps the following may be additional clarification on writing and using a callback function, especially if you wonder where it gets its arguments from and whether you can access its return values (if there is no way to get it from the function that takes the callback function).

    The following code defines a class CallBack that has two callback methods (functions) my_callback_sum and my_callback_multiply. The callback methods are fed into the method foo.

    # understanding callback
    
    class CallBack:
    
        @classmethod
        def my_callback_sum(cls, c_value1, c_value2):
            value = c_value1 + c_value2
            print(f'in my_callback_sum --> {c_value1} + {c_value2} = {value}')
            cls.operator = '+'
            return cls.operator, value
    
        @classmethod
        def my_callback_multiply(cls, c_value1, c_value2):
            value = c_value1 * c_value2
            print(f'in my_callback_multiply --> {c_value1} * {c_value2} = {value}')
            cls.operator = '*'
            return cls.operator, value
    
        @staticmethod
        def foo(foo_value, callback):
            _, value = callback(10, foo_value)
            # note foo only returns the value not the operator from callback!
            return value
    
    
    if __name__ == '__main__':
        cb = CallBack()
    
        value = cb.foo(20, cb.my_callback_sum)
        print(f'in main --> {value} and the operator is {cb.operator}')
    
        value = cb.foo(20, cb.my_callback_multiply)
        print(f'in main --> {value} and the operator is {cb.operator}')
    

    result:

    in my_callback_sum --> 10 + 20 = 30
    in main --> 30 and the operator is +
    in my_callback_multiply --> 10 * 20 = 200 
    in main --> 200 and the operator is *
    

    As you can see one value for the callback function c_value2 it gets from argument foo_value in foo and given in main the value 20, while c_value1 it gets internally from foo in this case the value 10 (and may be not clearly visible if foo is some method of a third party imported module, like pyaudio).

    The return value of the callback function functions can be retrieved by adding it to the namespace of the class CallBack, in this case cls.operator

    0 讨论(0)
  • 2021-02-04 06:31

    The problem is that you're evaluating the callback before you pass it as a callable. One flexible way to solve the problem would be this:

    def callback1(a, b):
        print('Sum = {0}'.format(a+b))
    
    def callback2(a):
        print('Square = {0}'.format(a**2))
    
    def callback3():
        print('Hello, world!')
    
    def main(callback=None, cargs=()):
        print('Calling callback.')
        if callback != None:
            callback(*cargs)
    
    main(callback1, cargs=(1, 2))
    main(callback2, cargs=(2,))
    main(callback3)
    

    Optionally you may want to include a way to support keyword arguments.

    0 讨论(0)
  • 2021-02-04 06:36

    In this code

    if callback != None:
        callback
    

    callback on its own doesn't do anything; it accepts parameters - def callback(a, b):

    The fact that you did callback(1, 2) first will call that function, thereby printing Sum = 3.

    Since callback returns no explicit value, it is returned as None.

    Thus, your code is equivalent to

    callback(1, 2)
    main()
    

    Solution

    You could try not calling the function at first and just passing its handle.

    def callback(sum):
        print("Sum = {}".format(sum))
    
    def main(a, b, _callback = None):
        print("adding {} + {}".format(a, b))
        if _callback:
            _callback(a+b)
    
    main(1, 2, callback)
    
    0 讨论(0)
提交回复
热议问题