Python function handle ala Matlab

前端 未结 3 901
面向向阳花
面向向阳花 2021-02-13 19:19

In MATLAB it is possible to create function handles with something like

myfun=@(arglist)body

This way you can create functions on the go withou

3条回答
  •  礼貌的吻别
    2021-02-13 19:49

    Turns out there is something that goes all the way back to 2.5 called function partials that are pretty much the exact analogy to function handles.

    from functools import partial
    def myfun(*args, first="first default", second="second default", third="third default"):
        for arg in args:
            print(arg)
        print("first: " + str(first))
        print("second: " + str(second))
        print("third: " + str(third))
    
    mypart = partial(myfun, 1, 2, 3, first="partial first")
    
    mypart(4, 5, second="new second")
    1
    2
    3
    4
    5
    first: partial first
    second: new second
    third: third default
    

提交回复
热议问题