Python function handle ala Matlab

前端 未结 3 900
面向向阳花
面向向阳花 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 20:01

    This is not quite the full answer. In matlab, one can make a file called funct.m:

    function funct(a,b)
       disp(a*b)
    end
    

    At the command line:

    >> funct(2,3)
         6
    

    Then, one can create a function handle such as:

    >> myfunct = @(b)funct(10,b))
    

    Then one can do:

       >> myfunct(3)
           30
    

    A full answer would tell how to do this in python.

    Here is how to do it:

    def funct(a,b):
        print(a*b)
    

    Then:

    myfunct = lambda b: funct(10,b)
    

    Finally:

    >>> myfunct(3)
    30
    

提交回复
热议问题