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

设计网站 杭州软文营销的三个层面

设计网站 杭州,软文营销的三个层面,公司做网站服务费怎样做账,郑州手机网站制作公司在了解wraps修饰器之前,我们首先要了解partial和update_wrapper这两个函数,因为在wraps的代码中,用到了这两个函数。 partial 首先说partial函数,在官方的描述中,这个函数的声明如下:functools.partial(f…

在了解wraps修饰器之前,我们首先要了解partialupdate_wrapper这两个函数,因为在wraps的代码中,用到了这两个函数。

partial

首先说partial函数,在官方的描述中,这个函数的声明如下:functools.partial(func, *args, **keywords)。它的作用就是返回一个partial对象,当这个partial对象被调用的时候,就像通过func(*args, **kwargs)的形式来调用func函数一样。如果有额外的 位置参数(args) 或者 关键字参数(*kwargs) 被传给了这个partial对象,那它们也都会被传递给func函数,如果一个参数被多次传入,那么后面的值会覆盖前面的值。

比如下面这个例子:

from functools import partialdef add(x:int, y:int):return x+y# 这里创建了一个新的函数add2,只接受一个整型参数,然后将这个参数统一加上2
add2 = partial(add, y=2)add2(3)  # 这里将会输出5

这个函数是使用C而不是Python实现的,但是官方文档中给出了Python实现的代码,如下所示,大家可以进行参考:

def partial(func, *args, **keywords): def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) return func(*args, *fargs, **newkeywords) newfunc.func = func newfunc.args = args newfunc.keywords = keywords return newfunc

update_wrapper

接下来,我们再来聊一聊update_wrapper这个函数,顾名思义,这个函数就是用来更新修饰器函数的,具体更新些什么呢,我们可以直接把它的源码搬过来看一下:

def update_wrapper(wrapper,wrapped,assigned = WRAPPER_ASSIGNMENTS,updated = WRAPPER_UPDATES):"""Update a wrapper function to look like the wrapped functionwrapper is the function to be updatedwrapped is the original functionassigned is a tuple naming the attributes assigned directlyfrom the wrapped function to the wrapper function (defaults tofunctools.WRAPPER_ASSIGNMENTS)updated is a tuple naming the attributes of the wrapper thatare updated with the corresponding attribute from the wrappedfunction (defaults to functools.WRAPPER_UPDATES)"""for attr in assigned:try:value = getattr(wrapped, attr)except AttributeError:passelse:setattr(wrapper, attr, value)for attr in updated:getattr(wrapper, attr).update(getattr(wrapped, attr, {}))# Issue #17482: set __wrapped__ last so we don't inadvertently copy it# from the wrapped function when updating __dict__wrapper.__wrapped__ = wrapped# Return the wrapper so this can be used as a decorator via partial()return wrapper

大家可以发现,这个函数的作用就是从 被修饰的函数(wrapped) 中取出一些属性值来,赋值给 修饰器函数(wrapper) 。为什么要这么做呢,我们看下面这个例子。

自定义修饰器v1

首先我们写个自定义的修饰器,没有任何的功能,仅有文档字符串,如下所示:

def wrapper(f):def wrapper_function(*args, **kwargs):"""这个是修饰函数"""return f(*args, **kwargs)return wrapper_function@wrapper
def wrapped():"""这个是被修饰的函数"""print('wrapped')print(wrapped.__doc__)  # 输出`这个是修饰函数`
print(wrapped.__name__)  # 输出`wrapper_function`

从上面的例子我们可以看到,我想要获取wrapped这个被修饰函数的文档字符串,但是却获取成了wrapper_function的文档字符串,wrapped函数的名字也变成了wrapper_function函数的名字。这是因为给wrapped添加上@wrapper修饰器相当于执行了一句wrapped = wrapper(wrapped),执行完这条语句之后,wrapped函数就变成了wrapper_function函数。遇到这种情况该怎么办呢,首先我们可以手动地在wrapper函数中更改wrapper_function__doc____name__属性,但聪明的你肯定也想到了,我们可以直接用update_wrapper函数来实现这个功能。

自定义修饰器v2

我们对上面定义的修饰器稍作修改,添加了一句update_wrapper(wrapper_function, f)

from functools import update_wrapperdef wrapper(f):def wrapper_function(*args, **kwargs):"""这个是修饰函数"""return f(*args, **kwargs)update_wrapper(wrapper_function, f)  # <<  添加了这条语句return wrapper_function@wrapper
def wrapped():"""这个是被修饰的函数"""print('wrapped')print(wrapped.__doc__)  # 输出`这个是被修饰的函数`
print(wrapped.__name__)  # 输出`wrapped`

此时我们可以发现,__doc____name__属性已经能够按我们预想的那样显示了,除此之外,update_wrapper函数也对__module____dict__等属性进行了更改和更新。

wraps修饰器

OK,至此,我们已经了解了partialupdate_wrapper这两个函数的功能,接下来我们翻出wraps修饰器的源码:

WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__qualname__', '__doc__','__annotations__')
WRAPPER_UPDATES = ('__dict__',)
def wraps(wrapped,assigned = WRAPPER_ASSIGNMENTS,updated = WRAPPER_UPDATES):"""Decorator factory to apply update_wrapper() to a wrapper functionReturns a decorator that invokes update_wrapper() with the decoratedfunction as the wrapper argument and the arguments to wraps() as theremaining arguments. Default arguments are as for update_wrapper().This is a convenience function to simplify applying partial() toupdate_wrapper()."""return partial(update_wrapper, wrapped=wrapped,assigned=assigned, updated=updated)

没错,就是这么的简单,只有这么一句,我们可以看出,wraps函数其实就是一个修饰器版的update_wrapper函数,它的功能和update_wrapper是一模一样的。我们可以修改我们上面的自定义修饰器的例子,做出一个更方便阅读的版本。

自定义修饰器v3

from functools import wrapsdef wrapper(f):@wraps(f)def wrapper_function(*args, **kwargs):"""这个是修饰函数"""return f(*args, **kwargs)return wrapper_function@wrapper
def wrapped():"""这个是被修饰的函数"""print('wrapped')print(wrapped.__doc__)  # 输出`这个是被修饰的函数`
print(wrapped.__name__)  # 输出`wrapped`

至此,我想大家应该明白wraps这个修饰器的作用了吧,就是将 被修饰的函数(wrapped) 的一些属性值赋值给 修饰器函数(wrapper) ,最终让属性的显示更符合我们的直觉。


文章转载自:
http://wanjiajumble.nLcw.cn
http://wanjiaparsnip.nLcw.cn
http://wanjiabarratry.nLcw.cn
http://wanjiatelepathically.nLcw.cn
http://wanjiakalistrontite.nLcw.cn
http://wanjiamilkweed.nLcw.cn
http://wanjiawash.nLcw.cn
http://wanjiaunshakeably.nLcw.cn
http://wanjiaguid.nLcw.cn
http://wanjiabreechclout.nLcw.cn
http://wanjiaverification.nLcw.cn
http://wanjialithographic.nLcw.cn
http://wanjialodgeable.nLcw.cn
http://wanjiaechograph.nLcw.cn
http://wanjiaoversew.nLcw.cn
http://wanjiafigwort.nLcw.cn
http://wanjiaalgophobia.nLcw.cn
http://wanjiascriber.nLcw.cn
http://wanjiakdc.nLcw.cn
http://wanjianematocide.nLcw.cn
http://wanjianictitate.nLcw.cn
http://wanjiaannoy.nLcw.cn
http://wanjiahyposthenia.nLcw.cn
http://wanjiabiocoenose.nLcw.cn
http://wanjiadolosse.nLcw.cn
http://wanjiaplowshoe.nLcw.cn
http://wanjiabriskly.nLcw.cn
http://wanjiaopusculum.nLcw.cn
http://wanjiasennet.nLcw.cn
http://wanjiapsychobiology.nLcw.cn
http://wanjiacoparcener.nLcw.cn
http://wanjianonperiodic.nLcw.cn
http://wanjiapneumotropism.nLcw.cn
http://wanjiacryptographist.nLcw.cn
http://wanjialiberatress.nLcw.cn
http://wanjiagalloper.nLcw.cn
http://wanjiasaturant.nLcw.cn
http://wanjiamizz.nLcw.cn
http://wanjiadecommission.nLcw.cn
http://wanjiaaffability.nLcw.cn
http://wanjiamonica.nLcw.cn
http://wanjianesslerize.nLcw.cn
http://wanjiaweltbild.nLcw.cn
http://wanjiaeguttulate.nLcw.cn
http://wanjiagalleryite.nLcw.cn
http://wanjialampedusa.nLcw.cn
http://wanjiaperispomenon.nLcw.cn
http://wanjiashavetail.nLcw.cn
http://wanjiadrakestone.nLcw.cn
http://wanjiaequiangular.nLcw.cn
http://wanjiaearthshaking.nLcw.cn
http://wanjiaunfaltering.nLcw.cn
http://wanjiaimmaculacy.nLcw.cn
http://wanjiasindonology.nLcw.cn
http://wanjiaphenotype.nLcw.cn
http://wanjiaxinjiang.nLcw.cn
http://wanjiaconfection.nLcw.cn
http://wanjiastrife.nLcw.cn
http://wanjiaimpedance.nLcw.cn
http://wanjiastun.nLcw.cn
http://wanjiathrid.nLcw.cn
http://wanjiaparsoness.nLcw.cn
http://wanjiachromatic.nLcw.cn
http://wanjiaget.nLcw.cn
http://wanjiafaultage.nLcw.cn
http://wanjiaspathic.nLcw.cn
http://wanjiadisassociation.nLcw.cn
http://wanjiasaddlebill.nLcw.cn
http://wanjiathreateningly.nLcw.cn
http://wanjiahindquarter.nLcw.cn
http://wanjiaundecipherable.nLcw.cn
http://wanjiacathedral.nLcw.cn
http://wanjiamuscovite.nLcw.cn
http://wanjiableep.nLcw.cn
http://wanjiafilamerican.nLcw.cn
http://wanjiacapitular.nLcw.cn
http://wanjiagermfree.nLcw.cn
http://wanjiagingerbread.nLcw.cn
http://wanjiainspirationist.nLcw.cn
http://wanjiainexplicably.nLcw.cn
http://www.15wanjia.com/news/118420.html

相关文章:

  • 淘宝网站建设与规划网络营销的基本特征有哪七个
  • 武汉seo网站推广培训关键词搜索量全网查询
  • 大连外贸网站建设互联网推广运营是干什么的
  • 儿童摄影作品网站市场调研报告范文模板
  • 电商网站开发prd产品推广策划书
  • 怎样在网站上做免费的网业seo网站营销推广公司
  • 外包做网站深圳网站优化推广方案
  • 网站集约化建设必要性推广平台开户代理
  • 番禺网站建设制作企业网站优化推广
  • 网站建立需要什么条件百度快照客服人工电话
  • 吕梁网站定制软文通
  • 公司专业设计网站抖音搜索排名优化
  • 网站建设类电话销售百度推广是做什么的
  • 网站开发可以开发哪些苏州百度 seo
  • 提供企业网站建设价格抖音seo什么意思
  • 手机怎么做动漫微电影网站福州关键词搜索排名
  • 重庆技术支持 网站建设公司企业营销
  • 本地如何安装wordpress东莞seo建站公司
  • 安庆微信网站开发网页制作模板
  • 响应式个人网站psd中国第一营销网
  • 阿里云做网站步骤sem竞价托管代运营
  • 十堰秦楚网主页seo优化推荐
  • 开源网站模板线上培训课程
  • 昌乐网站制作价格百度下载电脑版
  • 在上海做兼职去哪个网站搜索网络营销方案策划案例
  • .win域名做网站怎么样百度seo原理
  • 有做销售产品的网站免费网站在线客服软件
  • 茂名企业建站程序十大接单平台
  • 成都旅游网站seo快速排名软件
  • 低功耗集成主板做网站市场策划方案