dede网站地图模版廊坊网页模板建站
2026/1/15 19:28:19 网站建设 项目流程
dede网站地图模版,廊坊网页模板建站,交易猫假网站制作,南宁网站搜索引擎优化Python学习笔记-Day9-装饰器篇 函数的使用 首先回顾一下前面学过的函数#xff1a; def hi(nameyasoob): return hi name greet hi print(greet())在python中#xff0c;一切皆对象 在函数里定义函数 #方法一、 def a(name youzi):print(youzi)def greet():return no…Python学习笔记-Day9-装饰器篇函数的使用首先回顾一下前面学过的函数def hi(nameyasoob): return hi name greet hi print(greet())在python中一切皆对象在函数里定义函数#方法一、 def a(name youzi): print(youzi) def greet(): return now greet() function def welcome(): return now welcome() function print(greet()) print(welcome()) print(now a() function) hi() #方法二、 #这里如果也可以写成 def a(nameyouzi): def greet(): return now greet() function def welcome(): return now welcome() function if name youzi: return greet else: return welcome num a(nameddd) num1 a(nameyouzi) print(num()) print(num1())这里解释方法二细节nameyouzi’是默认参数当不填的时候就是youziretrun greet这里greet是对象当加上括号就调用执行了这个函数num a(name‘ddd’)实例化a然后传入参数ddd并赋值给num然后再通过判断传入的值来返回相应的值其实如果当函数不加括号就可以到处传递并且可以赋值给别的变量而不去执行它。这里其实我们如果调用函数不传参的话默认就是youzi然后就会返回greet这个时候我们可以这么写a()()就是调用greet当我把num a(name‘zhangsan’)那么就会返回welcome函数重要点函数里面的函数如果没有调用就不会被执行只有调用的时候才会执行函数里面的函数只能在函数里面的函数调用不能在函数的外部调用从函数中返回函数其实就是调用函数变量也可以接收函数但是函数一般分别两种一种是带括号的叫调用函数并且执行另一种是不带括号的叫对象#定义一个函数名叫hi def hi(): #返回的是hi yasoob return hi yasoob #定义一个函数但是因为我们今天要讲的是装饰器所以func这个的作用其实是用来接收函数的当然也可以接收变量、字典、列表参数等 def doSomethingBeforeHi(func): print(I am doing some boring work before executing hi()) a func() #这个其实是调用我们传过来的函数因为加了括号 print(a) doSomethingBeforeHi(hi)函数里定义函数然后在函数里调用函数双层嵌套函数并且调用另一个函数def a_new_decorator(a_func): def wrapTheFunction(): print(I am doing some boring work before executing a_func()) #调用传递过来的函数 a_func() print(I am doing some boring work after executing a_func()) #这个return的作用在于触发函数并且调用函数 return wrapTheFunction def a_function_requiring_decoration(): print(I am the function which needs some decoration to remove my foul smell) a_function_requiring_decoration() #print(a_new_decorator(a_function_requiring_decoration)) a_function_requiring_decoration a_new_decorator(a_function_requiring_decoration) a_function_requiring_decoration()总结可以函数里面定义函数当然也可以相互传递函数函数的参数也可以接收函数接下来开始进装饰器这个时候讲装饰器就会好理解点博客当时学这个也是很头大不知道为什么这样做后面发现之后感觉还挺有意思的符号来使用装饰器这里先提前解释在装饰函数的时候其实就已经做了一个动作就是把下面的函数名传给了装饰的函数定义的装饰函数如果不理解接着往下看演示def a_new_decorator(a_func): def wrapTheFunction(): print(I am doing some boring work before executing a_func()) a_func() print(I am doing some boring work after executing a_func()) return wrapTheFunction a_new_decorator def a_function_requiring_decoration(): print(I am the function which needs some decoration to remove my foul smell) #把a_function_requiring_decoration放到a_new_decorator装饰但是装饰不会调用 a_new_decorator(a_function_requiring_decoration) a_function_requiring_decoration()def a_new_decorator(a_func): def wrapTheFunction(): print(I am doing some boring work before executing a_func()) a_func() print(I am doing some boring work after executing a_func()) return wrapTheFunction a_new_decorator def a_function_requiring_decoration(): print(I am the function which needs some decoration to remove my foul smell) print(a_function_requiring_decoration.__name__) wrapTheFunction 封装函数 这里我们想打印原函数的名字所以接下来我们需要使用wraps使用wraps来保持原函数的名字#从functools库导入函数 from functools import wraps #定义一个函数添加一个参数f用来接收函数 def decorator_name(f): #让原函数保持不变 wraps(f) #这个是语法规范参数是固定死的函数名不是可以随便改但是要符合命名规范 def decorated(*args,**kwargs): #判断can_run如果是True就不执行是false就执行 if not can_run: return Functions will not run #把原函数的参数和输入的内容都返回回去 return f(*args,**kwargs) #返回decorated作用是调用并执行函数 return decorated #使用装饰器 decorator_name def func(): return (Functions is running) can_run True print(func()) can_run False print(func())return func(*args,**kwargs) 的核心作用动态调用函数func可以是任何函数传递所有参数*args传递所有位置参数也就是元组**kwargs传递所有关键字参数也就是字典保持透明性装饰器不需要知道被装饰函数的具体函数返回结果将原始函数的返回值原样返回闭包再说一个知识点那就是闭包解释通俗来说闭包就是函数及其周边环境的组合。在本文语境下你可以暂时理解周边环境就是一些外部的变量闭包通常涉及到一个内部函数内部函数可以访问或者说记住外部函数的局部变量即使外部函数已经执行完毕。代码如下# 创建一个装饰器名为simple_decorator里面func可以理解为参数也是函数 def simple_decorator(func): def wrapper(): print(--------开始-------) func() print(--------结束-------) #触发wrapper也就是调用该函数 return wrapper def greet(): print(您好) closure simple_decorator(greet) print(type(closure))closure就是一个闭包的案例print(--------开始-------) func() print(--------结束-------) 本质上执行的函数代码 print(--------开始-------) greet() print(--------结束-------)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询