python day 12学习 装饰器、生成器和模块
1、有参装饰器
1.有参装饰器的功能
# 在给函数添加功能的时候可以通过参数控制具体的操作(操作不固定)
# 2.怎么写有参装饰器
"""
def 函数名0(装饰器的参数列表)
def 函数名1(func):
def 函数名2(*args, **kwargs):
result = func(*arges, **kwargs)
新功能
return result
return 函数名2
return 函数名1
def 函数名(装饰器的参数列表)
无参装饰器
return 无参装饰器的函数名
有参装饰器的用法:
@函数名0(装饰器实参列表)
"""
# 写一个装饰器可以在函数结束后打印指定的任意提示信息
def add_message(msg):
def test1(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
print(msg)
return result
return new_func
return test1
@add_message('====after====')
def func1(x, y):
print(x+y)
func1(10, 20)
# 练习:写一个装饰器,在原函数返回值的基础上减去指定的值
# @sub(10) @sub(30)
def sub(num):
def test(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
if type(result) in (int, float, complex):
return result - num
return result
return new_func
return test
2、迭代器
1.有参装饰器的功能
# 在给函数添加功能的时候可以通过参数控制具体的操作(操作不固定)
# 2.怎么写有参装饰器
"""
def 函数名0(装饰器的参数列表)
def 函数名1(func):
def 函数名2(*args, **kwargs):
result = func(*arges, **kwargs)
新功能
return result
return 函数名2
return 函数名1
def 函数名(装饰器的参数列表)
无参装饰器
return 无参装饰器的函数名
有参装饰器的用法:
@函数名0(装饰器实参列表)
"""
# 写一个装饰器可以在函数结束后打印指定的任意提示信息
def add_message(msg):
def test1(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
print(msg)
return result
return new_func
return test1
@add_message('====after====')
def func1(x, y):
print(x+y)
func1(10, 20)
# 练习:写一个装饰器,在原函数返回值的基础上减去指定的值
# @sub(10) @sub(30)
def sub(num):
def test(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
if type(result) in (int, float, complex):
return result - num
return result
return new_func
return test
3、生成器
1.有参装饰器的功能
# 在给函数添加功能的时候可以通过参数控制具体的操作(操作不固定)
# 2.怎么写有参装饰器
"""
def 函数名0(装饰器的参数列表)
def 函数名1(func):
def 函数名2(*args, **kwargs):
result = func(*arges, **kwargs)
新功能
return result
return 函数名2
return 函数名1
def 函数名(装饰器的参数列表)
无参装饰器
return 无参装饰器的函数名
有参装饰器的用法:
@函数名0(装饰器实参列表)
"""
# 写一个装饰器可以在函数结束后打印指定的任意提示信息
def add_message(msg):
def test1(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
print(msg)
return result
return new_func
return test1
@add_message('====after====')
def func1(x, y):
print(x+y)
func1(10, 20)
# 练习:写一个装饰器,在原函数返回值的基础上减去指定的值
# @sub(10) @sub(30)
def sub(num):
def test(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
if type(result) in (int, float, complex):
return result - num
return result
return new_func
return test
4、模块的使用
1.有参装饰器的功能
# 在给函数添加功能的时候可以通过参数控制具体的操作(操作不固定)
# 2.怎么写有参装饰器
"""
def 函数名0(装饰器的参数列表)
def 函数名1(func):
def 函数名2(*args, **kwargs):
result = func(*arges, **kwargs)
新功能
return result
return 函数名2
return 函数名1
def 函数名(装饰器的参数列表)
无参装饰器
return 无参装饰器的函数名
有参装饰器的用法:
@函数名0(装饰器实参列表)
"""
# 写一个装饰器可以在函数结束后打印指定的任意提示信息
def add_message(msg):
def test1(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
print(msg)
return result
return new_func
return test1
@add_message('====after====')
def func1(x, y):
print(x+y)
func1(10, 20)
# 练习:写一个装饰器,在原函数返回值的基础上减去指定的值
# @sub(10) @sub(30)
def sub(num):
def test(func):
def new_func(*args, **kwargs):
result = func(*args, **kwargs)
if type(result) in (int, float, complex):
return result - num
return result
return new_func
return test