python装饰器的定义及用法

发布时间:2021-08-31 16:44:28 作者:chen
来源:亿速云 阅读:240

本篇内容主要讲解“python装饰器的定义及用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python装饰器的定义及用法”吧!

  定义:

  本质是函数(装饰其他函数),就是为其他函数添加附加功能。

  原则:

  不能修改被装饰的函数的源代码

  不能修改被装饰函数的调用方式

  先看一个完整的装饰器的例子:

  # Author: Mr.Xue

  # 2019.10.23

  """

  实现装饰的知识储备:

  1、函数即‘变量’

  2、高阶函数

  3、嵌套函数

  高阶函数+嵌套函数--->装饰器

  """

  import time

  def timmer(func):

  def warpper(*args, **kwargs):

  start_time = time.time()

  func()

  stop_time = time.time()

  print('the func run time is %s' % (stop_time - start_time))

  return warpper

  @timmer

  def test1():

  time.sleep(3)

  print("in the func test1")

  test1()

  1、函数即“变量”

  函数在内存中的存储机制:在内存中存储函数体,函数名作为门牌号

  注:

  python中的回收机制:不管是函数还是变量,一但"门牌号"不存在了,内存空间就会被回收

  2、高阶函数

  符合下面两个条件任何一个的就是高阶函数:

  把一个函数名作为实参传给另一个函数

  返回值中包含函数名

  '''

  第一种高阶函数:

  把一个函数名作为实参传给另一个函数

  --> 实现了不修改被装饰的函数bar代码的情况下为其添加新功能

  '''

  import time

  def bar():

  time.sleep(3)

  print('in the bar')

  def test(func):

  start_time = time.time()

  print(func)

  func() # run function bar

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  test(bar)

  #x = bar # 函数即'变量'

  #x()

  '''

  第二种高阶函数:

  返回值中包含函数名

  --> 不修改函数的调用方式

  '''

  import time

  def bar():

  time.sleep(3)

  print('in the bar')

  def test2(func):

  print(func) # func的内存地址

  return func #返回func的内存地址

  print(test2(bar))

  bar = test2(bar)

  bar()

  3、嵌套函数

  在一个函数的函数体中还有一个用def声明函数

  def foo():

  print("in the foo")

  def bar():

  print("in the bar")

  bar()

  foo()

  好啦,理解了上面的三个知识点之后,我们来一步步写一个装饰器,首先我们先定义两个函数test1、test2,再给两个函数添加新的功能,计算这两个函数的运行时间,怎么做呢,先定义一个高阶函数(上面提到的第一种高阶函数),将这两个函数当作参数传进去。

  import time

  def deco(func):

  start_time = time.time()

  func() # run function bar

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  def test1():

  time.sleep(3)

  print('in the test1')

  def test2():

  time.sleep(3)

  print('in the test2')

  deco(test1)

  deco(test2)

  现在我们完成了装饰器的原则之一,再不修改源代码的基础上给函数添加了新的功能。

  看上去好像不难呀,接下来,只要我们改进代码,完成不修改函数的调用方式的这个原则,是不是就完成了呢,好,那我们将上面的高阶函数改写成第二种的高阶函数

  import time

  def deco(func):

  start_time = time.time()

  return func # 返回func函数的内存地址

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  def test1():

  time.sleep(3)

  print('in the test1')

  def test2():

  time.sleep(3)

  print('in the test2')

  test1 = deco(test1)

  test2 = deco(test2)

  test1() #没有修改调用方式,新功能却不见了

  test2() #没有修改调用方式

  利用了第二种高阶函数之后,我们完成了装饰器的另外一个原则,没有改变函数的调用方式,但是发现新功能却没有加上去,这时候只用高阶函数来实现装饰器好像有点困难啊,怎么办呢?好像进入死循环了呀。

  不知道大家注意到没有,在上面那个装饰器的例子里面,有一行写着,“高阶函数+嵌套函数—>装饰器”,哦,那是不是我们再搞个嵌套函数就可以了呢,按照这个思路,我们继续往下走。

  import time

  def timer(func):

  def deco():

  start_time = time.time()

  func() # 调用func函数

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  return deco

  def test1():

  time.sleep(3)

  print('in the test1')

  def test2():

  time.sleep(3)

  print('in the test2')

  test1 = timer(test1)

  test2 = timer(test2)

  test1() #没有修改调用方式,新功能也加上去了

  test2() #没有修改调用方式,新功能也加上去了

  哇哦,现在就已经完成了装饰器了,只不过调用方式看上去比较lower而已,只要修改成标准写法就ok了。

  怎么做呢,在要被装饰的函数头部,加上"@装饰器名字",就好啦。

  import time

  def timer(func):

  def deco():

  start_time = time.time()

  func() # 调用func函数

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  return deco

  @timer # test1 = timer(test1)

  def test1():

  time.sleep(3)

  print('in the test1')

  @timer

  def test2():

  time.sleep(3)

  print('in the test2')

  test1()

  test2()

  只是现在我们写的这个装饰器和上面那个还有一点区别,我们的装饰器没有参数,而上面的那个却有,接下来,我们继续完善完善。郑州人流多少钱 http://mobile.zyyyzz.com/

  给我们的内嵌函数加上两个非固定参数,这样就可以接收参数了,如下:

  import time

  def timer(func):

  def deco(*args, **kwargs):

  start_time = time.time()

  func(*args, **kwargs) # 调用func函数

  stop_time = time.time()

  print("the func run time is %s" %(stop_time-start_time))

  return deco

  @timer # test1 = timer(test1)

  def test1():

  time.sleep(3)

  print('in the test1')

  @timer

  def test2(name, age):

  time.sleep(3)

  print('in the test2', name, age)

  test1()

  test2("xue", 24)

  ok, all finish!

  现在我们用装饰器写一个网站页面的需求:比如说,一共20个网站,其中除了首页不需要验证可以直接显示外,其他的都需要验证用户名密码才可以显示。

  # Author: Mr.Xue

  # 2019.10.24

  import time

  user, passwd = 'xue', 'abc123'

  def auth(auth_type):

  print("auth_type: ", auth_type)

  def outer_warpper(func):

  def warpper(*args, **kwargs):

  print("warpper func args:", *args, **kwargs)

  if auth_type == 'local':

  username = input("Username: ")

  password = input("Password: ")

  if user == username and passwd == password:

  print("\033[32;1mUser has passed authentication\033[0m")

  res = func(*args, **kwargs)

  return res

  else:

  print("\033[31;1mInvalid username or password\033[0m")

  elif auth_type == 'ldap':

  print('bu hui gao mao xian')

  return warpper

  return outer_warpper

  def index():

  print("in the index")

  @auth(auth_type='local') #home = warpper(home) -> warpper

  def home():

  print("in the home")

  return 'from home'

  @auth(auth_type='ldap')

  def bbs():

  print('in the bbs')

  index()

  print(home())

  bbs()

到此,相信大家对“python装饰器的定义及用法”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. Python装饰器高级用法
  2. Python中的装饰器的用法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Spring事务的详细介绍

下一篇:Java IO文件后缀名的过滤总结

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》