python学习day-6 内置函数 1

一、作用域
1、作用域即范围 - 全局范围(内置名称空间与全局名称空间属于该范围):全局存活,全局有效   - 局部范围(局部名称空间属于该范围):临时存活,局部有效 2、作用域关系是在函数定义阶段就已经固定的,与函数的调用位置无关,如下 name='alex'

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


r1 = foo()
r2 = r1() # tt
r3 = r2()
foo()()()
3、查看作用域:globals(),locals() LEGB 代表名字查找顺序: locals -> enclosing function -> globals -> __builtins__ locals 是函数内的名字空间,包括局部变量和形参 enclosing 外部嵌套函数的名字空间(闭包中常见) globals 全局变量,函数定义所在模块的名字空间 builtins 内置模块的名字空间

二、匿名函数
匿名就是没有名字 def func(x,y,z=1): return x+y+z lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使用一次就释放,除非让其有名字,让其有名字又没有意义了 func=lambda x,y,z=1:x+y+z func(1,2,3)
三、高阶函数
1.函数接收的参数是一个函数名  2#返回值中包含函数
把函数当作参数传给另外一个函数
def foo(n): #n=bar
print(n)

def bar(name):
print('my name is %s' %name)

# foo(bar)
# foo(bar())
foo(bar('alex'))

2.返回值中包含函数
def bar():
print('from bar')
def foo():
print('from foo')
return bar
n=foo()
n()
def hanle():
print('from handle')
return hanle
h=hanle()
h()



def test1():
print('from test1')
def test2():
print('from handle')
return test1()
四、内置函数
1.map函数 处理序列中的每个元素,得到的结果是一个‘列表’,该‘列表’元素个数及位置与原来一样
arrary=[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(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)
print('内置函数map,处理结果',res)
# for i in res:
# print(i) 一次释放后,就会输出空列表
print(list(res))
print('传的是有名函数',list(map(reduce_one,num_l)))


msg='linhaifeng'
print(list(map(lambda x:x.upper(),msg)))

2.filter函数 filter遍历序列中的每个元素,判断每个元素得到布尔值,如果是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))


print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

三种输出结果为:
<filter object at 0x0000000002DBF240>
['linhaifeng']
['linhaifeng']


实例二
people=[
{'name':'alex','age':1000},
{'name':'wupei','age':10000},
{'name':'yuanhao','age':9000},
{'name':'linhaifeng','age':18},
]
print(list(filter(lambda p:p['age']<=18,people)))




3.reduce函数 处理一个序列,然后把序列进行合并操作
from functools import reduce
num_l=[1,2,3,100]
print(reduce(lambda x,y:x+y,num_l,1))
print(reduce(lambda x,y:x+y,num_l))
 
4.all:对传入的值,进行布尔运算,全为真或者空就返回True,否则为Flase
5.abs取绝对值
6.any只要有一个是真,就返回True
7.bin 转二进制
8.bool 判断布尔值
#空,None,0的布尔值为False,其余都为True
print(bool(''))
print(bool(None))
print(bool(0))
9.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不能编码中文

10.chr 转换为ascii码
11.dir 某一个对象下边都有哪些方法
12.divmod 得(商,余数)
print(divmod(10,3)) 可用作分页功能
13.eval 将字符串里的数据类型剔除出来;把字符串中的表达式进行运算
14.可hash的数据类型即不可变数据类型,不可hash的数据类型即可变数据类型
15.help帮助函数
16.
print(bin(10))#10进制->2进制
print(hex(12))#10进制->16进制
print(oct(12))#10进制->8进制
17.
print(isinstance(1,int))
print(isinstance('abc',str))
print(isinstance([],list))
print(isinstance({},dict))
print(isinstance({1,2},set))
18.global locals
name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陈'
print(globals())
print(__file__)

def test():
age='1111111111111111111111111111111111111111111111111111111111111'
# print(globals())
print(locals())

test()

 
 


复制代码

猜你喜欢

转载自www.cnblogs.com/wangxiaoyienough/p/9260484.html
今日推荐