Passing more arguments to this type of python function

前端 未结 2 1405
北荒
北荒 2021-01-15 11:20

I figure this is pretty basic, but can\'t seem to figure out even how to ask google the right question. I am using this python websocket client to make some websocket conne

相关标签:
2条回答
  • 2021-01-15 12:14

    You can use a lambda to wrap the call:

    ws.on_open = lambda *x: on_open("this new arg", *x)
    
    0 讨论(0)
  • 2021-01-15 12:18

    Keep in mind that you need to assign a callback. You are instead calling a function and passing the return value to ws, which is incorrect.

    You can use functools.partial to curry a function to a higher order one:

    from functools import partial
    
    func = partial(on_open, "this new arg")
    ws.on_open = func
    

    When func is invoked, it will invoke on_open with the first argument as "this new arg", followed by any other arguments passed to func. Look at the implementation of partial in the doclink for more details.

    0 讨论(0)
提交回复
热议问题