作用域练习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))

scoping作用域,anonymous function匿名函数,built-in functions内置函数的更多相关文章

  1. [JSP][JSTL]页面调用函数--它${fn:}内置函数、是推断字符串是空的、更换车厢

    页面中调用函数--之${fn:}内置函数 函数描写叙述 fn:contains(string, substring) 假设參数string中包括參数substring,返回true fn:contai ...

  2. 【Python 函数对象 命名空间与作用域 闭包函数 装饰器 迭代器 内置函数】

    一.函数对象 函数(Function)作为程序语言中不可或缺的一部分,但函数作为第一类对象(First-Class Object)却是 Python 函数的一大特性. 那到底什么是第一类对象(Firs ...

  3. Python--函数对象@命名空间与作用域@包函数@装饰器@迭代器@内置函数

    一.函数对象 函数(Function)作为程序语言中不可或缺的一部分,但函数作为第一类对象(First-Class Object)却是 Python 函数的一大特性. 那到底什么是第一类对象(Firs ...

  4. day16 函数的用法:内置函数,匿名函数

    思维导图需要补全 : 一共有68个内置函数: #内置:python自带 # def func(): # a = 1 # b = 2 # print(locals()) # print(globals( ...

  5. python基础--递归、三元表达式、列表(字典)生成式、匿名函数、常用的内置函数

    函数的递归:函数在调用阶段直接或者间接的又调用自身 递归的两个阶段: 1.回溯:就是一次次重复的过程,这个重复的过程必须建立在每一次重复问题的复杂度都是应该下降的,直接有一个最终的结束条件(这个结束条 ...

  6. Day3 - Python基础3 函数、递归、内置函数

    Python之路,Day3 - Python基础3   本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8. ...

  7. 第七篇 python基础之函数,递归,内置函数

    一 数学定义的函数与python中的函数 初中数学函数定义:一般的,在一个变化过程中,如果有两个变量x和y,并且对于x的每一个确定的值,y都有唯一确定的值与其对应,那么我们就把x称为自变量,把y称为因 ...

  8. Python基础3 函数、递归、内置函数

    本节内容 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 温故知新 1. 集合 主要作用: 去重 关系测 ...

  9. Python之路【第四篇】: 函数、递归、内置函数

    一. 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警,你掏空了所有的知识量,写出了以下代码 while True: i ...

随机推荐

  1. 下载并配置jdk

    ①下载jdk安装到本机,这里是jdk8下载地址,请根据自己机子的环境进行下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...

  2. arch Linux(二)

    配置你的基本系统 下列是基于该视频4:40s的流水- 切换到普通用户: [root@eric-laptop ~]# su eric 查看系统信息: [eric@eric-laptop root]$ n ...

  3. win2012R2安装net4.6.2失败提示“更新2919355包问题,或者win8.1、win10”的错误

    前言 在客户的服务器电脑安装net4.6,提示安装失败错误,最后顺利成功安装net4.6. 一.错误 1.win2012R2安装net4.6.2失败提示“更新2919355包问题,或者win8.1.w ...

  4. Failed to process import candidates for configuration class [com.simple.....]

    主要原因: 是因为自己定制的starter在打包时(package)用了spring-boot-maven-plugin,即在你的定制starter工程的pom.xml中有如下配置: <buil ...

  5. [poj P2976] Dropping tests

    [poj P2976] Dropping tests Time Limit: 1000MS  Memory Limit: 65536K Description In a certain course, ...

  6. Angular - - ng-focus、ng-blur

    1.ng-focus 这个指令功能就是比如当一个input等获取到焦点的时候,执行你指定的表达式函数,达到你需要的目的 格式:ng-focus=“value” value:获取焦点时执行的表达式,方法 ...

  7. ajax请求本地文件

    这是一个小随笔,真的很简短! 主要入坑点有两个 一.Chrome浏览器默认不支持ajax读取本地文件 解决:1.关闭所有Chrome网页 2.右击Chrome浏览器,打开“属性”     3.弹出属性 ...

  8. CCF关于NOIP竞赛程序提交的管理规则

    在NOIP复赛中,NOI各省组织单位必须严格遵循CCF<关于NOIP数据提交格式的说明>的规范在竞赛结束后规定时间内向CCF提交本赛区所有参赛选手的程序. 为竞赛的公平以及赛后按时完成竞赛 ...

  9. 入坑deep learning 1

    想体验一下跑keras的感觉,按照这个小妹妹的教程:https://zhuanlan.zhihu.com/p/28333410 0. 大概花了十来个小时才搞定初步的小环境 1. 在linux 16.0 ...

  10. lenet-5,Alexnet详解以及tensorflow代码实现

    http://blog.csdn.net/OliverkingLi/article/details/73849228