栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

这个python装饰器如何工作?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

这个python装饰器如何工作?

关于装饰器的基本事实是

@decoratordef func(): ...

完全等同于

def func(): ...func=decorator(func)

所以,

@logger.logdef first_x_method(self): ...

是相同的

def first_x_method(self): ...first_x_method=logger.log(first_x_method)

因此,

logger.log
静态方法用arguments调用
func = first_x_method

在调用中

logger.log(first_x_method)
,子方法
__
已定义并返回。

first_x_method=logger.log(first_x_method)
因此设置
first_x_method
为引用sub方法
__

中的括号

first_x_method()
告诉Python调用该方法
first_x_method

因此,

x().first_x_method()
首先实例化类x的实例,然后调用方法
first_x_method
(提供的x()作为第一个参数)。

既然

first_x_method
指的
__
__
被调用。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号