一、有参装饰器(封装三层的闭包函数)
举两个有参函数例子
def outter2(xxx):
def outter(func):
def wrapper(*args, **kwargs):
xxx
res = func(*args, **kwargs)
return res
return wrapper
return outter
@outter2(xxx=111)
def index():
pass
def outter2(mode):
def outter(func):
def wrapper(*args, **kwargs):
inp_name = input("username:")
inp_pwd = input("password:")
if mode == "file":
if inp_name == 'lu' and inp_pwd == '345':
res = func(*args, **kwargs)
return res
elif mode == 'mysql':
print('基于mysql的数据库认证')
elif mode == 'ldap':
print('基于ladp的数据库认证')
else:
print('未知的数据来源')
return wrapper
return outter
@outter2('ldap')
def index():
print('from index')
index()
举例说明wraps装饰器
from functools import wraps
def outter(func):
@wraps(func)
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return res
#wrapper.__name__ = func.__name__
#wrapper.__doc__ = func.__doc__
return wrapper
@outter
def index():
"""
这是一个很厉害的函数
"""
print('index===>')
help(index)
print(index)
index()
总结:装饰器模板 1、无参装饰器模板
def outter(func):
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return res
return wrapper
@outter
def index():
pass
2、有参装饰器模板
def outter2(xxx):
def outter(func):
def wrapper(*args, **kwargs):
xxx
res = func(*args, **kwargs)
return res
return wrapper
return outter
@outter2(xxx=111)
def index():
pass
二、迭代器 1.什么是迭代器? 迭代器是迭代取值的一种工具 迭代是一个重复的过程,但不是单纯的重复,每次重复都是基于上一次的结果而来 2.为什么用迭代器? 要创建一种通用的统一的取值工具,不依赖于索引和key的取值方式 3.如何用迭代器? (1)可迭代的对象iterable 1、内置有 iter 方法 (2)迭代器对象iterator 1、内置有 iter 方法 2、内置有 next 方法 调用: 可迭代对象. iter () --> 返回迭代器对象 调用:迭代器对象. next () --> 返回的是下一个值
六个对象:列表,字典,元祖,集合,文件,字符串 #涉及到遍历出来的对象 均是可迭代对象 文件对象既是可迭代对象,又是迭代器对象
迭代器
dic = {'name': 'holmes', 'age': '18', 'gentle': 'male'}
dic1 = dic.__iter__()
print(dic1.__next__())
print(dic1.__next__())
print(dic1.__next__())
dic = {'name': 'egon', 'age': 18, 'gender': 'male'}
len("hello") # "hello".__len__()
iter_dic = iter(dic) # dic.__iter__()
while True:
try:
print(next(iter_dic))
except StopIteration:
break
for k in dic:
print(k)
dic = {'name': 'egon', 'age': 18, 'gender': 'male'}
iter_dic = iter(dic)
for k in iter_dic:
print(k)
print('='*50)
for k in iter_dic:
print(k)
for k in dic:
print(k)
print('='*50)
for k in dic:
print(k)
总结迭代器对象优缺点: 优点: (1) 提供了一种新的、统一的取值方式(可以不依赖于索引以及key的) (2) 惰性计算,不耗费内存 缺点: (1) 取值不够灵活 (2) 一次性的,只能往后取,无法预知数据的个数
三、生成器
def func():
print('hello1')
yield 1
print('hello2')
yield 2
print('hello3')
yield 3
g = func()
print(g)
res1 = next(g)
print(res1)
res2 = next(g)
print(res2)
res3 = next(g)
print(res3)
next(g)
函数内但凡出现yield关键字,再调用函数不会执行函数体代码,会得到一个生成器对象 生成器就是一种自定义的迭代器 yiled VS return: 相同点:返回值层面都一样 不同点:return只能返回一次值函数就立即结束,而yield可以返回多次值
例子 迭代器从零到无穷
def func():
start = 0
while True:
yield start
start += 1
res = func()
print(res)
print(next(res))
print(next(res))
print(next(res))
例子 迭代器range函数
def my_range(start, stop, step=1):
while start < stop:
yield start
start += step
for i in my_range(1, 7, 2): # 1 3 5
print(i)
四、面向过程编程 面向过程编程是一种编写思想or编程范式 面向过程核心是“过程”二字,过程就是流程,流程指的就是先干什么、再干什么、后干什么基于面向过程编写程序就好比在设计一条条流水线 优点:复杂的问题流程化、进而简单化 缺点:牵一发而动全身,扩展性差
五、表达式(生成式) 1、列表生成式
nums = []
for i in range(1, 6):
nums.append(i ** 2)
nums = [i ** 2 for i in range(1, 6)]
nums = ['ok' for i in range(1, 6)]
print(nums)
names = ['egon_nb', "lxx_sb", "hxx_sb"]
new_l = []
for name in names:
if name.endswith("_sb"):
new_l.append(name)
new_l = [name for name in names if name.endswith("_sb")]
print(new_l)
2、字典生成式
res = {i:i**2 for i in range(5)}
print(res,type(res))
items = [('k1',111),('k2',222)]
print({k:v for k,v in items})
3、集合生成式
res = {i for i in range(5)}
print(res,type(res))
4、生成器表达式
res = (i for i in range(5))
print(res,type(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
print(next(res))
for i in res:
print(i)
for i in res:
print(i)
for i in res:
print(i)
print(list(res))
print(list(res))
print(sum([1,2,3,4,5]))
with open('a.txt',mode='rt',encoding='utf-8') as f:
data = f.read()
print(len(data)) # 24
res = 0
for line in f:
res += len(line)
lines_size = (len(line) for line in f)
res = sum(lines_size)
res = sum(len(line) for line in f)
print(res)