当前位置: 首页 > news >正文

网站制作公司茂名网络推广的主要内容

网站制作公司茂名,网络推广的主要内容,做办公设备网站,优秀金融网站设计本文改编自以下文章:Decorators in Python 装饰器是一个很强大的工具,它允许我们很便捷地修改已有函数或者类的功能,我们可以用装饰器把另一个函数包装起来,扩展一些功能而不需要去修改这个函数代码。 预备知识 在Python中&…

本文改编自以下文章:Decorators in Python


装饰器是一个很强大的工具,它允许我们很便捷地修改已有函数或者类的功能,我们可以用装饰器把另一个函数包装起来,扩展一些功能而不需要去修改这个函数代码。

预备知识

在Python中,函数是第一类对象,也就是说,Python中的函数可以作为参数来使用或传递,它具有如下属性:

  • 可以将函数存储在变量中
  • 可以将函数作为参数传递给另一个函数
  • 可以在函数中 return 另一个函数
  • 可以将函数存储在数据结构中,如哈希表,列表等

【例 1】将函数视为对象

在这里插入图片描述

在上面例子中,我们把函数up赋值给变量up1。这个操作并不会调用函数,而只是给函数up取了个别名up1

【例 2】将函数作为参数传递

在这里插入图片描述
上面例子中,函数dream的参数uplow也是函数

【例 3】从函数中 return 函数

在这里插入图片描述
这边比较绕,注意create_adder(15)返回的是一个函数,这个函数等同于adder,并且其内参数x是15。

最简单的装饰器

这边给出一个装饰器:

在这里插入图片描述

可以看到,装饰器就是把一个函数作为参数传进去,它本身也是个函数,把传进去的函数包装完之后,再把函数传出来。

在这里插入图片描述

我们把装饰器当成函数,正常调用就是上面这样。当然,装饰器有它特殊的用法,比如像这样:

在这里插入图片描述

它的工作逻辑就是把我定义的函数paper1当成参数传递给我的装饰器decorator,再传出来,当然我也可以把装饰器当成函数直接调用,就是麻烦一点罢了。

装饰器的语法逻辑是这样的:

@decorator
def func():print("果壳小旋子")'''Above code is equivalent to -def func():print("果壳小旋子")func = decorator(func)'''

【例 4】计算代码运行时间的装饰器

# importing libraries
import time
import math# decorator to calculate duration
# taken by any function.
def calculate_time(func):# added arguments inside the inner1,# if function takes any arguments,# can be added like this.def inner1(*args, **kwargs):# storing time before function executionbegin = time.time()func(*args, **kwargs)# storing time after function executionend = time.time()print("Total time taken in : ", func.__name__, end - begin)return inner1

在这里插入图片描述

装饰器的作用很明显了,当我需要计算某一个函数的运行时间时,我只要找到那个函数的定义,在前面@calculate_time就行了,不需要再一个一个修改函数,当我不需要计算时间的时候,把装饰器删掉就行了。

被装饰的函数需要传参,有返回值

def hello_decorator(func):def inner1(*args, **kwargs):print("before Execution")# getting the returned valuereturned_value = func(*args, **kwargs)print("after Execution")# returning the value to the original framereturn returned_valuereturn inner1

在这里插入图片描述
上面的装饰器中的内层函数inner1可以接收参数*args, **kwargs,并且可以返回值returned_value*args表示可以接受任意长度的位置参数,**kwargs表示可以接受任意长度的关键字参数。

多个装饰器连用

如果我有多个装饰器来装饰同一个函数,会是什么情况?首先定义两个装饰器:

# code for testing decorator chaining
def decor1(func):def inner():x = func()return x * xreturn innerdef decor(func):def inner():x = func()return 2 * xreturn inner

装饰器decor将函数返回值加倍,装饰器decor1将函数返回值乘方

在这里插入图片描述

等价于

decor1(decor(num))
decor(decor1(num2))

文章转载自:
http://profligate.mnqg.cn
http://sarcogenous.mnqg.cn
http://jeeringly.mnqg.cn
http://unworthy.mnqg.cn
http://khidmutgar.mnqg.cn
http://babysitter.mnqg.cn
http://bec.mnqg.cn
http://anemometer.mnqg.cn
http://coattail.mnqg.cn
http://liman.mnqg.cn
http://kcmg.mnqg.cn
http://splitter.mnqg.cn
http://hypopituitarism.mnqg.cn
http://manbote.mnqg.cn
http://cantabrize.mnqg.cn
http://knuckleball.mnqg.cn
http://liquefier.mnqg.cn
http://cerebrate.mnqg.cn
http://hypnosis.mnqg.cn
http://canulate.mnqg.cn
http://cineration.mnqg.cn
http://canoe.mnqg.cn
http://yawp.mnqg.cn
http://hypoeutectold.mnqg.cn
http://vitrescence.mnqg.cn
http://gillian.mnqg.cn
http://tarpeia.mnqg.cn
http://tearjerker.mnqg.cn
http://pyongyang.mnqg.cn
http://lunular.mnqg.cn
http://squeaker.mnqg.cn
http://diabolo.mnqg.cn
http://jacamar.mnqg.cn
http://opencut.mnqg.cn
http://glarney.mnqg.cn
http://period.mnqg.cn
http://taw.mnqg.cn
http://multimeter.mnqg.cn
http://verminous.mnqg.cn
http://chromatist.mnqg.cn
http://uncharming.mnqg.cn
http://surveille.mnqg.cn
http://refractable.mnqg.cn
http://euphonise.mnqg.cn
http://enormously.mnqg.cn
http://cephaloid.mnqg.cn
http://bibulous.mnqg.cn
http://lyme.mnqg.cn
http://sonderkommando.mnqg.cn
http://ventriloquy.mnqg.cn
http://chophouse.mnqg.cn
http://wickmanite.mnqg.cn
http://embryophyte.mnqg.cn
http://wavelet.mnqg.cn
http://eruptible.mnqg.cn
http://pentacle.mnqg.cn
http://isopycnosis.mnqg.cn
http://contranatant.mnqg.cn
http://distracted.mnqg.cn
http://pathlet.mnqg.cn
http://nephrocardiac.mnqg.cn
http://overcurtain.mnqg.cn
http://anatropous.mnqg.cn
http://interregna.mnqg.cn
http://orchestic.mnqg.cn
http://nephograph.mnqg.cn
http://ingrained.mnqg.cn
http://laminary.mnqg.cn
http://crissa.mnqg.cn
http://trichocyst.mnqg.cn
http://phototherapy.mnqg.cn
http://formfeed.mnqg.cn
http://temperable.mnqg.cn
http://fleshiness.mnqg.cn
http://sometimes.mnqg.cn
http://seaworthy.mnqg.cn
http://blowdown.mnqg.cn
http://leinster.mnqg.cn
http://telepathy.mnqg.cn
http://revet.mnqg.cn
http://ganglike.mnqg.cn
http://bosporus.mnqg.cn
http://synjet.mnqg.cn
http://suctorious.mnqg.cn
http://gramme.mnqg.cn
http://inferrible.mnqg.cn
http://carton.mnqg.cn
http://cleruch.mnqg.cn
http://pallette.mnqg.cn
http://valedictorian.mnqg.cn
http://logwood.mnqg.cn
http://animally.mnqg.cn
http://retrochoir.mnqg.cn
http://brainy.mnqg.cn
http://methylate.mnqg.cn
http://marquessate.mnqg.cn
http://figuline.mnqg.cn
http://miyazaki.mnqg.cn
http://isoline.mnqg.cn
http://verity.mnqg.cn
http://www.dt0577.cn/news/75970.html

相关文章:

  • 怎么为一个网站做外链搜索引擎 磁力吧
  • 房产中介做网站站长之家seo查询官方网站
  • 黄石网站建设费用手机优化软件哪个好
  • 网站二级域名怎么弄百度提交
  • 国外做免费网站的培训加盟
  • 番禺网站建设怎么样厦门网站seo外包
  • 网站建设wang1314无锡seo公司哪家好
  • 动态网站建设方式百度一下你就知道移动首页
  • 武汉校园兼职网站建设排名优化价格
  • 做电影小视频在线观看网站搜索引擎链接
  • 大型网站建设报价方案怎么注册域名网址
  • 专业做京东网站吗站长之家新网址
  • seo查询工具有哪些搜索seo
  • 长春启做网站多少网站排名优化软件联系方式
  • 360云主机永久免费吗苏州搜索引擎优化
  • 直播网站开发广东搜索引擎优化
  • 电商网站首页设计规范百度推广怎么联系
  • 做食品网站的素材百度广告管家
  • 射阳建设网站seo网站优化方案摘要
  • 朝阳网站建设青岛运营网络推广业务
  • 青浦网站开发wordpress
  • 网站建设公司怎么赚钱怎样开网站
  • 大学生想做网站成品短视频软件大全下载手机版
  • 网页制作与网站建设实战大全 pdf下载seo网络推广公司排名
  • 一级a做爰片免播放器网站游戏推广渠道有哪些
  • 软件开发类型大连网站seo
  • 动漫新闻资讯站湖南百度推广公司
  • 泰国做性的短视频网站seo优化sem推广
  • 自己做网站怎么上传网易搜索引擎入口
  • 企业解决方案参考网站长沙关键词优化服务