Advanced function

  1. Anonymous functions

def calc(x, y):
     return x+ y

func = lambda x, y: x+ y

print(calc(1, 2))
print(func(1, 2))

  format: lambda variable: logical

  Anonymous functions support up to ternary operations and cannot implement more complex logic

def calc(x, y):
    if x < y:
        return x+y
    else:
        return x - y


func = lambda x, y: x+y if x < y else x - y

print(calc(1, 2))
print(func(1, 2))
View Code

  Second, higher-order functions: accept one or more functions as parameters or return another function

 

  3. Recursion

 

  Fourth, the decorator

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738236&siteId=291194637