scoping作用域,anonymous function匿名函数,built-in functions内置函数

时间:2023-03-09 07:00:00
scoping作用域,anonymous function匿名函数,built-in functions内置函数

作用域练习1

def test1():
print('in the test1')
def test():
print('in the test')
return test1
res = test()
print(res()) #res = test1地址 函数没有return,默认返回None

作用域练习2

name = 'alex'
def foo():
name = 'lhf'
def bar():
name = 'wupeiqi'
print(name)
return bar
a = foo()
print(a)
a()

作用域练习3

name = 'alex'
def foo():
name = 'lhf'
def bar():
name = 'wupeiqi'
print(name)
def tt():
print(name)
return tt
return bar
bar = foo()
tt = bar()
print(tt)
print(tt())
#上面几句=print(foo()()())

lambda匿名函数

lambda匿名函数语法规则自定义函数名 = lambda 形参:要实现的功能子代码lambda函数是匿名函数,用来赋值给具体函数,在函数段中调用具体函数,‘:’后为实际功能代码例如:f = lambda x:name+'_sb'

 函数式编程

#把函数当作参数传给另一个函数
def foo(n):
print(n)
def bar(name):
print('my name is %s' %name)
foo(bar('alex'))

高阶函数

#高阶函数定义:满足 把函数当做参数传给另一个函数/返回值中包含函数
#函数式编程
#编程三方式:面向过程(详细写出函数编程各过程),面向函数(无变量赋值),面向对象
#把函数当作参数传给另一个函数
def foo(n):
print(n)
def bar(name):
print('my name is %s' %name)
foo(bar('alex')) # 返回值中包含函数
def bar():
print('from bar')
def foo():
print('from foo')
return bar
n = foo()
n()
def handle():
print('from handle')
return handle
h = handle()
h()

python所有内置函数:引用自网页。

举例应用:如bytes(),map(func(),*iteration), filter(func(),*iteration), reduce(func,*iteration,init),eval()

bytes()用什么方式编码,就用什么方式解码

# name='你好'
# print(bytes(name,encoding='utf-8'))
# print(bytes(name,encoding='utf-8').decode('utf-8'))
#
# print(bytes(name,encoding='gbk'))
# print(bytes(name,encoding='gbk').decode('gbk'))
# print(bytes(name,encoding='ascii'))#ascii不能编码中文

函数式编程,map内置函数用法(最终版)

map函数功能:map(func,*iteration)函数中两个参数分别代表:逻辑+函数,每次运行把后面的参数传到前面的逻辑运行)

num1 = [1,2,10,5,3,7]
def add_one(x):
return x+1
lambda x:x+1 这行代码等于上面两行
def reduce_one(x):
return x-1 num = [1,2,10,5,3,7]
def map_test(func,array):
ret = []
for i in array:
res = func(i)
ret.append(res)
return ret
print(num)
print(map_test(lambda x:x+1,num))
print(map_test(lambda x:x**2,num))
print(map_test(lambda x:x-1,num))print(map_test(lambda x:x+1,num))
res = map(lambda x:x+1,num)
print('内置函数map,处理结果',list(map(lambda x:x+1,num))) map举例,字符串
msg = 'yuyukun'
print(list(map(lambda x:x.upper(),msg))) filter函数用法,filter(func(),*iteration), 在函数中,filter()依次用for循环依次遍历iteration中的参数,用func()中的逻辑来判断,作bool运算,正确则返回True
movie_people = ['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
print(filter(lambda n:not n.endswith('sb'),movie_people))
res = filter(lambda n:not n.endswith('sb'),movie_people)
print(list(res)) reduce函数
num = [1,2,3,100]
def reduce_test(func,array,init=None):
if init == 0:
res = array.pop(0)
else:
res = init
for num in array:
res = func(res,num)
return res
print(reduce_test(lambda x,y:x*y,num,10))#10=初值
reduce函数 把两个完整的序列,合并并压缩到一起(如下就是初值1和num中元素加在一起)
from functools import reduce
num = [1,2,3,100]
print(reduce(lambda x,y:x*y,num,1))

max,min内置函数

l={1,2,3,4,5}
print(max(l))
print(min(l))

map,reduce,filter函数总结

# map()处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
num_l = [1,2,3,4,5]
def map_test(func,array): #func=lambda x:x+1 arrary=[1,2,10,5,3,7]
ret=[]
for i in array:
res=func(i) #add_one(i)
ret.append(res)
return ret
print(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)
print('内置函数map,处理结果',res)
print(list(res))
msg='linhaifeng'
print(list(map(lambda x:x.upper(),msg))) people=[
{'name':'alex','age':1000},
{'name':'wupeiqi','age':10000},
{'name':'yuanhao','age':5000},
{'name':'yuyukun','age':18}
]
#filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是True则留下来
print(list(filter(lambda p:p['age']<=18,people))) #reduce:处理一个序列,然后把序列进行合并操作
from functools import reduce
print(reduce(lambda x,y:x+y,range(1,2),1))
print(reduce(lambda x,y:x+y,range(100),100))

eval()函数功能,把字符串中的数据类型提取出来

# eval()功能1:提取字符串中的数据类型
dic = {'name':'alex'}
dic_str = str(dic)
print(dic_str)
print(eval(dic_str))
d1 = eval(dic_str)
print(d1['name'])
# eval()功能2:将字符串中的数学运算再进行一遍
express = '1+2*(3/3-1)-2'
print(express)
print(eval(express))