Fork me on GitHub

叠加多个装饰器

# 一、叠加多个装饰器的加载、运行分析(了解***)

# def deco1(func1): # func1 = wrapper2的内存地址
# def wrapper1(*args,**kwargs):
# print('正在运行===>deco1.wrapper1')
# res1=func1(*args,**kwargs)
# return res1
# return wrapper1

# def deco2(func2): # func2 = wrapper3的内存地址
# def wrapper2(*args,**kwargs):
# print('正在运行===>deco2.wrapper2')
# res2=func2(*args,**kwargs)
# return res2
# return wrapper2

# def deco3(x):
# def outter3(func3): # func3=被装饰对象index函数的内存地址
# def wrapper3(*args,**kwargs):
# print('正在运行===>deco3.outter3.wrapper3')
# res3=func3(*args,**kwargs)
# return res3
# return wrapper3
# return outter3

# 加载顺序自下而上(了解)
# @deco1 # index=deco1(wrapper2的内存地址) ===> index=wrapper1的内存地址
# @deco2 # index=deco2(wrapper3的内存地址) ===> index=wrapper2的内存地址
# @deco3(111) # ===>@outter3===> index=outter3(index) ===> index=wrapper3的内存地址
# def index(x,y):
# print('from index %s:%s' %(x,y))

# 执行顺序自上而下的,即wraper1-》wrapper2-》wrapper3
# index(1,2) # wrapper1(1,2)

yield表达式

# x=yield 返回值

# 一:
# def dog(name):
# print('道哥%s准备吃东西啦...' %name)
# while True:
# # x拿到的是yield接收到的值
# x = yield # x = '肉包子'
# print('道哥%s吃了 %s' %(name,x))
#
#
# g=dog('alex')
# g.send(None) # 等同于next(g)
#
# g.send(['一根骨头','aaa'])
# # g.send('肉包子')
# # g.send('一同泔水')
# # g.close()
# # g.send('1111') # 关闭之后无法传值

# 二:
# def dog(name):
# food_list=[]
# print('道哥%s准备吃东西啦...' %name)
# while True:
# # x拿到的是yield接收到的值
# x = yield food_list # x = '肉包子'
# print('道哥%s吃了 %s' %(name,x))
# food_list.append(x) # ['一根骨头','肉包子']
#
# g=dog('alex')
# res=g.send(None) # next(g)
# print(res)
#
# res=g.send('一根骨头')
# print(res)
#
# res=g.send('肉包子')
# print(res)
# # g.send('一同泔水')

# def func():
# print('start.....')
# x=yield 1111 # x='xxxxx'
# print('哈哈哈啊哈')
# print('哈哈哈啊哈')
# print('哈哈哈啊哈')
# print('哈哈哈啊哈')
# yield 22222

# g=func()
# res=next(g)
# print(res)
#
# res=g.send('xxxxx')
# print(res)

三元表达式

# 针对以下需求
# def func(x,y):
# if x > y:
# return x
# else:
# return y
#
# res=func(1,2)
# print(res)

# 三元表达式
# 语法格式: 条件成立时要返回的值 if 条件 else 条件不成立时要返回的值
# x=1
# y=2

# res=x if x > y else y
# print(res)

# res=111111 if 'egon' == 'egon' else 2222222222
# print(res)

# 应用举例
# def func():
# # if 1 > 3:
# # x=1
# # else:
# # x=3
#
# x = 1 if 1 > 3 else 3

生成式

# 1、列表生成式
# l = ['alex_dsb', 'lxx_dsb', 'wxx_dsb', "xxq_dsb", 'egon']
# new_l=[]
# for name in l:
# if name.endswith('dsb'):
# new_l.append(name)

# new_l=[name for name in l if name.endswith('dsb')]
# new_l=[name for name in l]

# print(new_l)

# 把所有小写字母全变成大写
# new_l=[name.upper() for name in l]
# print(new_l)

# 把所有的名字去掉后缀_dsb
# new_l=[name.replace('_dsb','') for name in l]
# print(new_l)

# 2、字典生成式
# keys=['name','age','gender']
# dic={key:None for key in keys}
# print(dic)

# items=[('name','egon'),('age',18),('gender','male')]
# res={k:v for k,v in items if k != 'gender'}
# print(res)

# 3、集合生成式
# keys=['name','age','gender']
# set1={key for key in keys}
# print(set1,type(set1))

# 4、生成器表达式
# g=(i for i in range(10) if i > 3)
# !!!!!!!!!!!强调!!!!!!!!!!!!!!!
# 此刻g内部一个值也没有

# print(g,type(g))

# print(g)
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))
# print(next(g))


# with open('笔记.txt', mode='rt', encoding='utf-8') as f:
# 方式一:
# res=0
# for line in f:
# res+=len(line)
# print(res)

# 方式二:
# res=sum([len(line) for line in f])
# print(res)

# 方式三 :效率最高
# res = sum((len(line) for line in f))
# 上述可以简写为如下形式
# res = sum(len(line) for line in f)
# print(res)

函数的递归

# 一:递归的定义
# 函数的递归调用:是函数嵌套调用的一种特殊形式
# 具体是指:
# 在调用一个函数的过程中又直接或者间接地调用到本身

# 直接调用本身
# def f1():
# print('是我是我还是我')
# f1()
# f1()

# 间接接调用本身
# def f1():
# print('===>f1')
# f2()
#
# def f2():
# print('===>f2')
# f1()
#
# f1()

# 一段代码的循环运行的方案有两种
# 方式一:while、for循环
# while True:
# print(1111)
# print(2222)
# print(3333)

# 方式二:递归的本质就是循环:
# def f1():
# print(1111)
# print(2222)
# print(3333)
# f1()
# f1()

# 二:需要强调的的一点是:
# 递归调用不应该无限地调用下去,必须在满足某种条件下结束递归调用
# n=0
# while n < 10:
# print(n)
# n+=1

# def f1(n):
# if n == 10:
# return
# print(n)
# n+=1
# f1(n)
#
# f1(0)

# 三:递归的两个阶段
# 回溯:一层一层调用下去
# 递推:满足某种结束条件,结束递归调用,然后一层一层返回

# age(5) = age(4) + 10
# age(4) = age(3) + 10
# age(3) = age(2) + 10
# age(2) = age(1) + 10
# age(1) = 18

# def age(n):
# if n == 1:
# return 18
# return age(n-1) + 10
#
#
# res=age(5)
# print(res)

# 四:递归的应用
# l=[1,2,[3,[4,[5,[6,[7,[8,[9,10,11,[12,[13,]]]]]]]]]]
#
# def f1(list1):
# for x in list1:
# if type(x) is list:
# # 如果是列表,应该再循环、再判断,即重新运行本身的代码
# f1(x)
# else:
# print(x)
#
# f1(l)
posted on 2020-03-25 14:46  OBOS  阅读(207)  评论(0编辑  收藏  举报