搜尋此網誌

2026年9月6日星期日

Lambda

def someFunc(var1, var2, var3, var4):

someFunc(var1=1,2,3,4)

This call is not valid Python syntax.

Once you start with a keyword argument (var1=1), every argument that follows must also be a keyword argument.

You can’t drop back into plain positional arguments afterward.


def someFunc(func):

    print(func(5) + 2)

someFunc(lambda x: x * 3)

Function definition

  • someFunc takes one argument, func.

  • Inside, it calls func(5) and then adds 2 to the result.

  • Finally, it prints that value.

Calling with a lambda

  • You pass in lambda x: x * 3.

  • That’s an anonymous function that multiplies its input by 3.

Execution

  • Inside someFunc, func(5) runs → 5 * 3 = 15.

  • Then 15 + 2 = 17.

This is a neat example of higher-order functions: you’re passing a function as an argument to another function.

Microsoft Copilot

沒有留言:

發佈留言