1. 程式人生 > >python_匿名函數

python_匿名函數

res num expr mic bsp 實例 lan ber G1

python 的 匿名函數

語法:lambda[arg1 [,arg2,.....argn]]:expression

如下實例:

    sum = lambda arg1, arg2: arg1 + arg2

    #調用sum函數
    print "Value of total : ", sum( 10, 20 )
    print "Value of total : ", sum( 20, 20 )

以上實例輸出結果:

Value of total : 30

    Value of total :  40

擴展: lambda 作為參數

  def test(a,b,func):


    result = func(a, b)
   return result

  func_new = input("請輸入一個匿名函數:")

  num = test(12,34,func_new)

  print(num)



python_匿名函數