【函数的作用域】
作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变
example1:
name='alex'
def foo():
name='lhf'
def bar():
print(name)
return bar #返回bar的地址
position = foo()
print(position) #打印bar的地址,输出<function foo.<locals>.bar at 0x0000014430AD2620>
position() #运行position这个地址上的函数(也就是bar),输出lhf
example2:
name='alex'
def foo():
name='lhf'
def bar():
name='wupeiqi'
def tt():
print(name)
return tt
return bar
func=foo()
func()() #wupeiqi
【匿名函数】
--------------先定义一个函数--------------
def calc(x):
return x+1
print(calc(10))
--------------可以把上面的函数写成匿名函数的形式--------------
lambda x:x+1
其中,x是形参,x+1是返回值
print(lambda x:x+1) #返回函数地址<function <lambda> at 0x0000015C650D06A8>
func = lambda x:x+1
print(func(10)) #11
PS:匿名函数通常和别的函数一同使用;匿名函数通常只能做简单的运算
匿名函数也可以返回多个值:
print((lambda x,y,z:(x+1,y+1,z+1))(1,2,3)) #(2, 3, 4)
【编程的方法论】
1、面向过程
2、函数式
3、面向对象
【面向过程编程】
特点:把一个大问题变成一个个小步骤,一步一步来
【函数式编程】
函数:编程语言定义的函数+数学意义定义的函数
特点:代码比较精简,但是可读性比较差,函数体内不能有任何的赋值过程
example1:函数接受的参数是一个函数名
def foo(n):
print(n)
def bar(name):
print('my name is %s' %name)
foo(bar('alex'))
#my name is alex
#None
example2:函数的返回值中包含函数
def bar():
print('from bar')
def foo():
print('from foo')
return bar
foo()()
#from foo
#from bar
------------------------------
def han():
print('from han')
return han
han()() #打印出两个from han
print(han()()) #打印出两个from han和一个han的地址
#from han
#from han
#from han
#from han
#<function han at 0x00000286C70BF6A8>
【高阶函数】
特点:包含以下两种情况之一的函数就叫高阶函数:1、函数接受的参数是一个函数名;2、函数的返回值中包含函数
【尾调用】
含义:在函数的最后一步调用另外一个函数(最后一行不一定是函数的最后一步)
example1:非尾调用
def bar(n):
return n
def foo(x):
return bar(x)+1 #这里的bar(x)不是尾调用
example2:尾调用
def bar(n):
return n
def foo(x):
return bar(x) #这里的bar(x)是尾调用
【map函数】
作用:
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
语法:
"""
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
"""
function -- 函数
iterable -- 一个或多个序列
Python 3.x 返回迭代器
example:
msg = 'abcdefghijklmn'
print(list(map(lambda x:x.upper(),msg)))
#['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N']
【filter函数】
注释:
"""
filter(function or None, iterable) --> filter object
Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
"""
example:
movie_people =['alex_sb','wupeiqi_sb','lihaifeng','yuanhao_sb']
print(filter(lambda x:x.endswith('sb'),movie_people))
print(list(filter(lambda x:x.endswith('sb'),movie_people)))
print(list(filter(lambda x:not x.endswith('sb'),movie_people)))
#<filter object at 0x000002094A294470>
#['alex_sb', 'wupeiqi_sb', 'yuanhao_sb']
#['lihaifeng']
【reduce函数】
python2和python3对比:
在python2中,reduce函数可以直接用,但是在python3中,reduce函数被集成进模块中了,需要调用模块才能使用reduce函数
注释:
"""
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
"""
example:
from functools import reduce #python2和python3中不同
num_l = [1,2,3,100]
print(reduce(lambda x,y:x+y,num_l))
print(reduce(lambda x,y:x+y,num_l,1))
#106
#107
【三个函数的比较】
map()
处理可迭代序列中的每一个元素,得到的结果是一个“列表”,该“列表”元素个数和原来一样
filter()
遍历序列中的每一个元素,判断每一个元素得到布尔值,如果是True则留下来
reduce()
处理一个序列,把序列进行合并操作