Python基础之函数:3、多层语法糖、装饰器和装饰器修复技术及递归函数
一、多层语法糖
1、什么是多层语法糖:
多层语法糖是指在单个源代码函数名上方添加了多个语法糖,使这段源代码函数体具备多个功能
2、多层语法糖用法:
首先定义好装饰器功能,将需要添加功能的函数体代码放置在装饰器下方,将需要执行功能的装饰器语法糖按照执行的顺序防在原函数体函数名上方,多层语法糖加载顺序由下往上
- 代码用法:
def outter1(func1):
print('加载了outter1')
def wrapper1(*args, **kwargs):
print('执行了wrapper1')
res1 = func1(*args, **kwargs)
return res1
return wrapper1
def outter2(func2):
print('加载了outter2')
def wrapper2(*args, **kwargs):
print('执行了wrapper2')
res2 = func2(*args, **kwargs)
return res2
return wrapper2
def outter3(func3):
print('加载了outter3')
def wrapper3(*args, **kwargs):
print('执行了wrapper3')
res3 = func3(*args, **kwargs)
return res3
return wrapper3
@outter1
@outter2
@outter3
def index():
print('from index')

二、有参装饰器
1、什么是有参装饰器:
有参装饰器是指在无参装饰器的基础的函数体外层再加上一层函数
2、有参装饰器的作用:
当无参函数体内需要局部名称需要外部传参时,我们就可以再有参装饰器函数名后方参数内进行传参
- 有参装饰器模板:
def 有参装饰器(x,y,z):
def outter(func):
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return res
return wrapper
return outter
@有参装饰器(1,y=2,z=3)
def 被装饰对象():
pass
- 有参装饰器实战用法:
def auth(db_type):
def deco(func):
def wrapper(*args, **kwargs):
name = input('your name>>>: ').strip()
pwd = input('your password>>>: ').strip()
if db_type == 'file':
print('基于文件的验证')
if name == 'egon' and pwd == '123':
res = func(*args, **kwargs) # index(1,2)
return res
else:
print('user or password error')
elif db_type == 'mysql':
print('基于mysql的验证')
elif db_type == 'ldap':
print('基于ldap的验证')
else:
print('不支持该db_type')
return wrapper
return deco
@auth(db_type='file') # @deco # index=deco(index) # index=wrapper
def index(x, y):
print('index->>%s:%s' % (x, y))
@auth(db_type='mysql') # @deco # home=deco(home) # home=wrapper
def home(name):
print('home->>%s' % name)
@auth(db_type='ldap') # 账号密码的来源是ldap
def transfer():
print('transfer')
# index(1, 2)
# home('egon')
# transfer()
三、装饰器修复技术
1、什么是装饰器修复技术:
装饰器修复技术是指,虽然我们再使用装饰器时,可以通过不改变源代码的调用方式和代码能够执行了新的功能,但我们调用的源代码函数名地址的用法并不是源代码的地址,这时我们就可以通过使用装饰件修复技术使调用的源文件地址和用法和源文件相同
- 代码表现
1.首先全局中调用装饰器修饰模块
from functools import wraps
2.再闭包函数或装饰器代码提中调用
@warps 需要修复的函数名
- 实战用法
from functools import wraps
def outer(func_name):
@wraps(func_name)
def inner(*args, **kwargs):
res = func_name(*args, **kwargs)
return res
return inner
@outer
def func():
print('我是func函数体代码')
func()
print(func)
help(func)
--------------------------------------------------------------------------
我是func函数体代码
<function func at 0x00000217883988B0>
Help on function func in module __main__:
func()
四、递归函数
1、什么是递归函数:
1.函数体代码内部调用自己本身函数名
2.两个函数体代码内部相互调用对方函数名
3.每一次调用能得出一个新的数据值,并且明确了结束时间
注意事项:
1.在递归函数未明确结束条件时,代码会进入死循环,这时会触发python保护机制,这段代码最多可运行1000次左右
- 代码表现
1.直接调用自己本身
count = 0
def func():
global count
count += 1
print(count)
return func()
func()
2.间接调用:
count = 0
def func():
global count
count += 1
print(count)
return func1()
def func1():
print(count)
return func()
func()
1、递归函数的作用:
1.是一种计算过程,可以通过递归函数进行数学运算
- 实战用法
'''
问:
小明第一天做了20个俯卧撑
以后每增加一天就会多做1个
求:
小米第100做多少个俯卧撑
'''
def func(n):
if n == 100:
return 20
return func(n + 1)+1
res = func(1)
print(res)
--------------------------------------------------------------------------
119
Python基础之函数:3、多层语法糖、装饰器和装饰器修复技术及递归函数的更多相关文章
- Python基础之函数:2、globlal与nonlocal和闭包函数、装饰器、语法糖
目录 一.global与nonlocal 1.global 2.nonlocal 二.函数名的多种用法 三.闭包函数 1.什么是闭包函数 2.闭包函数需满足的条件 3.闭包函数的作用 4.闭包函数的实 ...
- python基础之函数详解
Python基础之函数详解 目录 Python基础之函数详解 一.函数的定义 二.函数的调用 三.函数返回值 四.函数的参数 4.1 位置参数 4.2 关键字参数 实参:位置实参和关键字参数的混合使用 ...
- python基础——匿名函数及递归函数
python基础--匿名函数及递归函数 1 匿名函数语法 匿名函数lambda x: x * x实际上就是: def f(x): return x * x 关键字lambda表示匿名函数,冒号前面的x ...
- python基础——匿名函数
python基础——匿名函数 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中,对匿名函数提供了有限支持.还是以map()函数为例,计算f(x)=x2时 ...
- python基础——返回函数
python基础——返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_ ...
- python基础——sorted()函数
python基础——sorted()函数 排序算法 排序也是在程序中经常用到的算法.无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小.如果是数字,我们可以直接比较,但如果是字符串或者两个d ...
- python基础——filter函数
python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...
- 八. Python基础(8)--函数
八. Python基础(8)--函数 1 ● 函数返回布尔值 注意, 自定义的函数也可以是用来作逻辑判断的, 例如内置的startswith()等函数. def check_len(x): ' ...
- python全栈开发_day14_常见语法糖,递归和匿名函数
一:常见语法糖 1)三元函数(三目函数) a=1 if 3>2 else 2 print(a) #得到返回值:1 2)列表字典推导式 lis=[("a",1),(" ...
随机推荐
- 年轻的樵夫哟,你掉的是这个免费 8 核 4G 公网Docker 服务器
Play With Docker 直接打开 https://labs.ply-with-docker.com/ 即可访问 Play With Docker 平台. 注册一个 DockerHub 账号便 ...
- 回溯、贪心、DP的区别和联系
四大常用算法:分治.贪心.回溯.动态规划 回溯算法是个"万金油".基本上能用跟动态规划.贪心解决的问题,都可以用回溯去解决.回溯算法相当于穷举搜索,穷举所有情况,然后得到最优解.不 ...
- HDU4372 Count the Buildings (+题解:斯特林数)
题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...
- java数组---稀疏数组与数组之间的相互转化
public static void main(String[] args) { int[][]array1=new int[11][11]; array1[1][2]=1; array1[2][3] ...
- 面试突击78:@Autowired 和 @Resource 有什么区别?
@Autowired 和 @Resource 都是 Spring/Spring Boot 项目中,用来进行依赖注入的注解.它们都提供了将依赖对象注入到当前对象的功能,但二者却有众多不同,并且这也是常见 ...
- C# 数组 深拷贝 和 数组传参
前言 C#中引用类型无法使用const,因此传参的时候使用引用类型,一定要注意是否会改变其值.下面介绍几种 数组的 深拷贝方法. 前提 下面的测试代码有一些前提, sw为Stopwatch nForT ...
- React报错之Property 'value' does not exist on type EventTarget
正文从这开始~ 总览 当event参数的类型不正确时,会产生"Property 'value' does not exist on type EventTarget"错误.为了解决 ...
- C语言:多功能计算器
好家伙,这个东西有点折磨 这是一个多功能计算器 #include<stdio.h> #include<math.h> #include<windows.h> voi ...
- Docker_构建_运行总结
样例: 构建镜像 build-image-fim-backend.sh echo "开始构建 fim-backend 镜像..." cp -rp ../target/fim-bac ...
- spark 写入数据到Geomesa(Hbase)
package com.grady.geomesa import org.apache.spark.sql.jts.PointUDT import org.apache.spark.sql.types ...