Python_day4

 

Python

PHP

函数式编程

允许把函数本身作为参数传入另一个函数,还允许返回一个函数

 

高阶函数

abs(),函数的调用不是函数本身

作为函数本身,函数名其实也是一个变量,可以改变指向,不过一般不这么做函数名可以对变量进行赋值使变量也指向函数

variable = abs

高阶函数:能够将函数作为参数的函数

x = -5

y = 6

f = abs

print(add(x,y,f))

$a = "abs";

function test($a){

           echo $a(-2);

}

test($a);

map()

将函数作用到一个可迭代对象上

map(function,Iterable)

def f(x):

    return x*x

l = [1, 2, 3, 4, 5, 6, 7, 8, 9]

res = map(f,l)

print(list(res))

 

reduce()

从前两个开始,传入函数中,执行的返回值与第三个数继续传入,直到执行完毕

reduce(function,Iterable)

from functools import reduce

def p(x,y):

    return x*y

l = [1,2,3]

print(reduce(p,l))

最终结果是个数字

如果Iterable只有一个数,那直接返回该值

 

















猜你喜欢

转载自blog.csdn.net/zhq_zvik/article/details/80251339