当前位置:网站首页>(六)装饰器扩展之[email protected]使用原理
(六)装饰器扩展之[email protected]使用原理
2022-07-24 02:37:00 【覆水难收呀】
(一)@pytest.mark.XXX是怎么实现的?
玩过接口自动化的童鞋都用过pytest,基本都是用装饰器实现的,上几个文章我们讲了装饰器的使用原理,这次我们拓展一个新的知识点:@pytest.mark.XXX这个实现原理是什么?
import pytest
@pytest.mark.skip
def test():
pass我们举个例子 :
@pytest.mark.skip 实际上就是使用pytest模块里面的mark这个对象.skip方法
@模块.对象.方法
(二)我们来自己实现一个这种形式的装饰器
(1)我们新建一个lemon.py文件,创建一个Mark类,里面写了skip函数,skip函数里面有一个嵌套函数wrapper。

(2)再建一个demo1_装饰器扩展.py文件, 创建一个test2方法,然后@lemon.mark.skip, 最后在下面调用test2()试试。

打印结果:
----skip----1
------test2------
----skip----2
下面上源码给大家自己去试一下
from py09_07day import lemon
class Mark:
def skip(self, func):
def wrapper(*args, **kwargs):
print("----skip----1")
res = func()
print("----skip----2")
return res
return wrapper
mark = Mark()
# @lemon.mark.skip ===> @lemon这个类.mark对象.方法
# @skip test2 = skip(test2)
@lemon.mark.skip
def test2():
print('------test2------')
test2()
# 打印结果
----skip----1
------test2------
----skip----2边栏推荐
- Use the pagoda panel to plan tasks and automatically push the website to Baidu for inclusion
- Crud operation of mongodb (2)
- Chinese scientists have made new progress in high security quantum key distribution networks
- Réalisation d'un diagramme de zone de ligne brisée - Diagramme Rose - Diagramme à barres
- [diary of supplementary questions] [2022 Niuke summer school 1] i-chiitoitsu
- Recorded on July 21, 2022
- Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family
- NumberOptional:一个字符串转数字的工具
- Understand the timing of loading classes into the JVM
- C from zero
猜你喜欢

TP5 framework link promotion project

Qml- use listview to build a three-level treeview architecture
![[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat
![[untitled]](/img/61/91a8a67d069193a9f3000a43ccdab9.png)
[untitled]

Wallys/PD-60 802.3AT Input Output802.3AT/AT 85% Efficiency 10/100/1000M GE Surge Protection

Share an API Gateway project based on ABP and yarp

ssm的求职招聘系统兼职应聘求职

Opensmile introduction and problems encountered during installation

Composition API (in setup) watch usage details
![[untitled]](/img/57/a3104833cb5fcc05916075f3bfdaf3.png)
[untitled]
随机推荐
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
【补题日记】[2022牛客暑期多校1]I-Chiitoitsu
C from zero
Correlation
相关性(correlation)
Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire
IBM: realize the quantum advantage of fault tolerance by 2030
Digital transformation behind the reshaping growth of catering chain stores
Wechat applet setting background image does not display problem solution
Unity TimeLine使用教程
Codeworks 5 questions per day (average 1500) - day 23
508. 出现次数最多的子树元素和-哈希表法纯c实现
Mysql database, grouping function
Use the pagoda panel to plan tasks and automatically push the website to Baidu for inclusion
Wechat applet realizes broken line area chart rose chart three-dimensional histogram
【补题日记】[2022杭电暑期多校1]C-Backpack
7月开发过程中遇到的问题总结
Maximize, minimize, restore, close and move the WinForm form form of C #
Is Zhongcheng hospital really helping suppliers solve problems?
Why use the well architected framework?